Elasticsearch查询命令

添加映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT lagou
{
"mappings": {
"job": {
"properties": {
"title":{
"store":true,
"type": "text",
"analyzer": "ik_max_word"
},
"company_name":{
"store": true,
"type": "keyword"
},
"desc":{
"type": "text"
},
"comments":{
"type": "integer"
},
"add_time":{
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}

添加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
POST lagou/job/
{
"title":"python djiango开发",
"companya_name":"美团",
"desc":"对Django的概念熟悉,熟悉Python",
"comments":20,
"add_time":"2017-4-1"
}
POST lagou/job/
{
"title":"python开发",
"companya_name":"美团",
"desc":"对Django的概念熟悉,熟悉Python",
"comments":20,
"add_time":"2017-5-1"
}
POST lagou/job/
{
"title":"python 分布式爬虫开发",
"companya_name":"百度科技",
"desc":"对scrapy的概念熟悉,熟悉Python",
"comments":50,
"add_time":"2017-4-1"
}

查询命令

match查询 分词查询

1
2
3
4
5
6
7
8
GET lagou/_search
{
"query": {
"match": {
"title": "爬虫开发"
}
}
}

term查询 全量查询

1
2
3
4
5
6
7
8
GET lagou/_search
{
"query": {
"term": {
"title": "python"
}
}
}

terms查询

1
2
3
4
5
6
7
8
9
10
11
GET lagou/_search
{
"query": {
"terms": {
"title": [
"爬虫",
"开发"
]
}
}
}

match查询 分词查询 控制查询的返回数量

1
2
3
4
5
6
7
8
9
10
GET lagou/_search
{
"query": {
"match": {
"title": "开发"
}
},
"from": 0,
"size": 3
}

match_all查询

1
2
3
4
5
6
GET lagou/_search
{
"query": {
"match_all": {}
}
}

match_phrase查询 要满足所有词才返回结果

1
2
3
4
5
6
7
8
9
10
11
GET lagou/_search
{
"query": {
"match_phrase": {
"title": {
"query": "python开发",
"slop":10
}
}
}
}

multi_match 查询 fields中有满足即可

1
2
3
4
5
6
7
8
9
GET lagou/_search
{
"query": {
"multi_match": {
"query": "熟悉",
"fields": ["title","desc"]
}
}
}

返回store为true的字段

1
2
3
4
5
6
7
8
9
GET lagou/_search
{
"stored_fields": ["title","company_name"],
"query": {
"match": {
"title": "python"
}
}
}

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
GET lagou/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"comments": {
"order": "desc"
}
}
]
}

查询范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
GET lagou/_search
{
"query": {
"range": {
"comments": {
"gte": 10,
"lte": 50,
"boost": 2.0
}
}
}
}
GET lagou/_search
{
"query": {
"range": {
"add_time": {
"gte": "2017-04-01",
"lte": "now",
"boost": 2.0
}
}
}
}

wildcard查询 通配符模糊查询

1
2
3
4
5
6
7
8
9
10
GET lagou/_search
{
"query": {
"wildcard": {
"title": {
"value": "dj*o"
}
}
}
}

Bool查询

1
2
3
4
5
6
7
#bool包括 must shold must_not filter
#格式:
#bool:{
#"filter":[],
#"must":[],
#"shold":[],
#"must_not":[],

插入数据

1
2
3
4
5
6
7
8
9
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"}

薪资20K

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GET lagou/testjob/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"term": {
"salary": "30"
}
}
}
}
}

指定多个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
GET lagou/testjob/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
]
, "filter": {"terms": {
"salary": [10,30]
}}
}
}
}

select * from testjob where title=”Python”

1
2
3
4
5
6
7
8
9
10
11
12
13
GET lagou/testjob/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {"match":
{"title":"Python"}
}
}
}
}

查看分析结果

1
2
3
4
5
GET _analyze
{
"analyzer": "ik_smart",
"text":"Python开发工程师"
}

select * from testjob where(salary=20 OR title=Pyhon) AND (salary!=30)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
GET lagou/testjob/_search
{
"query": {
"bool": {
"should": [
{"term":{"salary":20}},
{"term":{"title":"python"}}
],
"must_not": [
{"term":{"salary":30}}
]
}
}
}

嵌套查询select * from testjob where title="python" or (title="django" AND salary=30)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
GET lagou/testjob/_search
{
"query": {
"bool": {
"should": [
{"term": {
"title":"python"
}},
{"bool": {
"must": [
{"term":{"title":"django"}},
{"term":{"salary":30}}
]
}}
]
}
}
}