1稍浆、elasticsearch安裝
2载碌、elasticsearch概念
3 、elasticsearch的crud衅枫、批量操作
4嫁艇、elasticsearch映射mapping
5、elasticsearch查詢
圖片.png
準(zhǔn)備
PUT lagou
{
"mappings": {
"job":{
"properties":{
"title":{
"type": "text",
"store":true,
"analyzer": "ik_max_word"
},
"company_name": {
"type": "keyword",
"store":true
},
"desc":{
"type":"text"
},
"add_time":{
"type":"date",
"format":"yyyy-MM-dd"
},
"comments":{
"type": "integer"
}
}
}
}
}
POST lagou/job
{
"title":"python django 開發(fā)工程師" ,
"company_name":"美國(guó)科技有限公司",
"desc":"對(duì)django的概念熟悉弦撩,熟悉python基礎(chǔ)知識(shí)",
"comments":20,
"add_time":"2017-04-01"
}
POST lagou/job
{
"title":"python scrapy redis 分布式爬蟲基本" ,
"company_name":"百度科技有限公司",
"desc":"對(duì)scrapy的概念熟悉步咪,熟悉redis的基本操作",
"comments":5,
"add_time":"2017-04-15"
}
POST lagou/job
{
"title":"Elasticsearch打造搜索引擎" ,
"company_name":"阿里巴巴科技有限公司",
"desc":"熟悉數(shù)據(jù)結(jié)構(gòu)算法,熟悉python的基本開發(fā)",
"comments":15,
"add_time":"2017-06-20"
}
POST lagou/job
{
"title":"python打造推薦引擎系統(tǒng)" ,
"company_name":"阿里巴巴科技有限公司",
"desc":"熟悉推薦引擎的原理以及算法益楼、掌握C語言",
"comments":60,
"add_time":"2016-10-20"
}
簡(jiǎn)單查詢
#查看分析器解析的結(jié)果
GET _analyze
{
"analyzer": "ik_smart",
"text":"Python網(wǎng)絡(luò)開發(fā)師"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text":"Python網(wǎng)絡(luò)開發(fā)師"
}
#match查詢 (分詞查詢) python 和分布式
#查詢第0-2條的title和company_name字段(desc字段的stored屬性不是true)猾漫,并按comments排序
GET lagou/_search
{
"stored_fields":["title","company_name","desc"],
"query":{
"match":{
"title":"python分布式"
}
}点晴,
"from": 0,
"size": 2,
"sort": [
{
"comments": {
"order": "desc"
}
}
]
}
#查詢comments在大于等于10、小于等于20悯周、權(quán)重2.0的數(shù)據(jù)
GET lagou/_search
{
"query":{
"range": {
"comments": {
"gte": 10,
"lte": 20,
"boost":2.0
}
}
}
}
GET lagou/_search
{
"query":{
"range": {
"add_time": {
"gte": "2017-04-01",
"lte": "now",
}
}
}
}
#term查詢(不會(huì)做處理粒督、直接查,類似于keyword屬性)
GET lagou/_search
{
"query":{
"term":{
"title":"python"
}
}
}
#terms 和用match查django分布工程 效果一樣
GET lagou/_search
{
"query":{
"terms":{
"title":["django" ,"分布" ,"工程" ]
}
}
}
#match_all
GET lagou/_search
{
"query":{
"match_all":{}
}
}
#match_phrase 滿足所有詞 既有python也有系統(tǒng)禽翼,倆個(gè)詞最小間距6位
GET lagou/_search
{
"query":{
"match_phrase": {
"title": {
"query": "python系統(tǒng)",
"slop":6
}
}
}
}
#multi_match 多字段匹配,title的權(quán)重高于desc的3倍
GET lagou/_search
{
"query":{
"multi_match": {
"query": "python系統(tǒng)",
"fields":["title^3","desc"]
}
}
}
#wildcard 通配符查詢
GET lagou/_search
{
"query":{
"wildcard": {
"title": {
"value": "pyth*n"
}
}
}
}
組合查詢
#bool 查詢
#用 bool 包括 must should must_not filter來完成
#格式如下
#bool:{
# "filter":[], #不參與打分
# "must":[], #相當(dāng)于 (salary=20 and title=Python)
# "should":[], #相當(dāng)于 (salary=20 or title=Python)
# "must_not":[], #相當(dāng)于not
#}
#建立測(cè)試數(shù)據(jù)
POST lagou/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}
DELETE lagou/testjob
#簡(jiǎn)單的過濾查詢
#最簡(jiǎn)單的fileter查詢
#select * from testjob where salary=20
GET lagou/testjob/_search
{
"query":{
"bool": {
"must":{
"match":{
"salary":10
}
},
"filter":{
"terms":{
"title":["Python"]
}
}
}
}
}
#select * from testjob
#where (salary=20 or title=Python) and salary!=30 and salary!=10
GET lagou/testjob/_search
{
"query":{
"bool": {
"should":[
{"term":{"salary":20}},
{"term":{"title":"python"}}
],
"must_not": [
{"term": {"salary": "30"}},
{"term": {"salary": "10"}}
]
}
}
}
#where (salary=30 and title="django") or title="python"
GET lagou/testjob/_search
{
"query":{
"bool": {
"should":[
{"term":{"title":"python"}},
{"bool": {
"must":[
{"term":{"salary":30}},
{"term":{"title":"django"}}
]
}}
]
}
}
}
#測(cè)試數(shù)據(jù)
POST lagou/testjob2/_bulk
{"index":{"_id":1}}
{"tags":["search"]}
{"index":{"_id":2}}
{"tags":["search","python"]}
{"index":{"_id":3}}
{"other_filed":["some data"]}
{"index":{"_id":4}}
{"tags":null}
{"index":{"_id":5}}
{"tags":["search",null]}
#處理null空值的方法
#select tags from testjob2 where tags is not null
GET lagou/testjob2/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "tags"
}
}
}
}
}
#select tags from testjob2 where tags is null
GET lagou/testjob2/_search
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "tags"
}
}
}
}
}