尊重原創:http://www.javashuo.com/article/p-qjdnfkca-kd.htmlhtml
單字段 模糊匹配查詢與精準查詢
postman請求 POST 127.0.0.1:9200/book/_search 請求json: { "query":{ "match":{ "name":"曉明9" } } } 注:match 模糊查詢的標識 :查詢內容自動拆分紅分詞來查詢
若match 改成 match_phrase :精準查詢 具體能夠查看 http://www.cnblogs.com/liuxiaoming123/p/8119217.html
響應結果: { "took": 51, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0.5753642, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.5753642, "_source": { "name": "曉明1", "country": "china1", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "5", "_score": 0.28363907, "_source": { "name": "曉明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 0.28363907, "_source": { "name": "曉明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 0.23911436, "_source": { "name": "曉明9", "country": "china9", "age": 26, "date": "1992-08-08" } } ] } }
多字段 模糊匹配查詢與精準查詢
postman請求URL: POST 127.0.0.1:9200/book/_search 請求json字符串: { "query":{ "multi_match":{ "query":"曉明china", "fields":["name","country"] } } } 注:multi_match爲指定多字段匹配
響應結果:
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.5753642,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 0.5753642,
"_source": {
"name": "曉明1",
"country": "china1",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 0.47000363,
"_source": {
"name": "曉明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.47000363,
"_source": {
"name": "曉明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 0.23911436,
"_source": {
"name": "曉明9",
"country": "china9",
"age": 26,
"date": "1992-08-08"
}
}
]
}
}
語法查詢
未指定字段:
postman請求: POST 127.0.0.1:9200/book/_search 請求json字符串: { "query":{ "query_string":{ "query":"(ElasticSearch AND 入門) OR SpringBoot" } } } 返回結果: { "took": 21, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 2.634553, "hits": [ { "_index": "book", "_type": "novel", "_id": "9", "_score": 2.634553, "_source": { "name": "ElasticSearch 入門", "country": "china9", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.2876821, "_source": { "name": "SpringBoot", "country": "china1", "age": 26, "date": "1992-08-08" } } ] } }
語法查詢
指定字段:spring
postman請求: POST 127.0.0.1:9200/book/_search 請求json字符串: { "query":{ "query_string":{ "query":"SpringBoot OR 中國", "fields":["name","country"] } } } 響應結果: { "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.8630463, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.8630463, "_source": { "name": "SpringBoot", "country": "中國", "age": 26, "date": "1992-08-08" } } ] } }
(結構化數據的查詢)json
指定字段查詢:(term)springboot
postman請求: POST 127.0.0.1:9200/book/_search 請求json字符串: { "query" : { "term" : {"name" : "springboot"} } } 響應結果: { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.2876821, "_source": { "name": "SpringBoot", "country": "中國", "age": 26, "date": "1992-08-08" } } ] } }
注:若查詢英文時 應全字母小寫 精確查詢post
若查詢中文時 應按單個字來查詢spa
範圍查詢:3d
注:json請求字符串中部分字段的含義code
range:範圍關鍵字htm
gte 大於等於blog
lte 小於等於
gt 大於
lt 小於
now 當前時間
postman請求: 127.0.0.1:9200/book/_search 請求json字符串: { "query" : { "range" : { "date" : { "gte":"2017-01-01", "lte":"now" } } } } 響應結果: { "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "book", "_type": "novel", "_id": "9", "_score": 1, "_source": { "name": "ElasticSearch 入門", "country": "china9", "age": 28, "date": "2017-08-08" } } ] } }
Filter Context(對數據進行過濾)
postman請求: POST 127.0.0.1:9200/book/_search 請求json字符串: { "query" : { "bool" : { "filter" : { "term":{ "age":20 } } } } } 響應結果: { "took": 24, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 0, "_source": { "name": "SpringBoot", "country": "中國", "age": 20, "date": "1992-08-08" } } ] } }
注: boost 固定響應結果分數的值
postman請求: POST 127.0.0.1:9200/_search 請求json字符串: { "query" : { "constant_score" : { "filter" : { "match":{ "name":"曉明" } }, "boost":2 } } } 響應結果: { "took": 11, "timed_out": false, "_shards": { "total": 8, "successful": 8, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 2, "hits": [ { "_index": "book", "_type": "novel", "_id": "5", "_score": 2, "_source": { "name": "曉明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 2, "_source": { "name": "曉明8", "country": "china", "age": 26, "date": "1992-08-08" } } ] } }
should關鍵詞:或的關係
若should改成must 關鍵詞則表示 和的關係
postman請求: POST 127.0.0.1:9200/_search 請求json字符串: 1.shuld: { "query" : { "bool" : { "should" : [ { "match":{"name":"springboot"} }, { "match":{"country":"中國"} } ] } } }
2.must: { "query" : { "bool" : { "must" : [ { "match":{"name":"springboot"} }, { "match":{"country":"中國"} } ] } } }
3.must filter: {
"query" : {
"bool" : {
"must" : [
{
"match":{"name":"springboot"}
},
{
"match":{"country":"中國"}
}
],
"filter":[
{
"term":{
"age":20
}
}
]
}
}
}
4.must_not:
{
"query" : {
"bool" : {
"must_not" : {
"term":{"age":20}
}
}
}
}