Elasticsearch 實戰Query DSL搜尋語法
技術標籤:Elasticsearch實戰elasticsearch
Elasticsearch實戰
本課時主要介紹以下三種語法
must 必須滿足;
should如果包含了must或者filter查詢,那麼should的查詢語句就不是或者的意思了,而是有或者沒有都行的含義;使用minimum_should_match
,至少匹配一項should
子句;
must_not 必須不滿足;
step1
準備三條介紹技術的文件
POST /technology/type/_bulk {"index":{"_id":1}} {"title":"ES搜尋引擎","content":"Elasticsearch是一個基於Lucene的搜尋伺服器。","creator":"Lucene","cost":60} {"index":{"_id":2}} {"title":"Solr搜尋引擎","content":"Solr是一個高效能,採用Java開發,基於Lucene的全文搜尋伺服器","creator":"Lucene","cost":20} {"index":{"_id":3}} {"title":"RocketMQ","content":"訊息佇列 RocketMQ 版(原ONS)是阿里雲基於 Apache RocketMQ構建的低延遲、高併發、高可用、高可靠的分散式訊息中介軟體。","creator":"Alibaba","cost":30}
step2
需求查詢‘’搜尋引擎“,內容可以包含“訊息佇列”也可以不包含,不可以來自Alibaba
GET /technology/type/_search { "query": { "bool": { "must": [ { "match": { "title": "搜尋引擎" } } ], "should": [ { "match": { "content": "訊息佇列" } } ], "must_not": [ { "match": { "creator": "Alibaba" } } ] } } }
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 530,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {"value" : 2,
"relation" : "eq"
},
"max_score" : 1.6365592,
"hits" : [
{
"_index" : "technology",
"_type" : "type",
"_id" : "1",
"_score" : 1.6365592,
"_source" : {
"title" : "ES搜尋引擎",
"content" : "Elasticsearch是一個基於Lucene的搜尋伺服器。",
"creator" : "Lucene",
"cost" : 60
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "2",
"_score" : 1.6365592,
"_source" : {
"title" : "Solr搜尋引擎",
"content" : "Solr是一個高效能,採用Java開發,基於Lucene的全文搜尋伺服器",
"creator" : "Lucene",
"cost" : 20
}
}
]
}
}
step3
使用"minimum_should_match": 1
GET /technology/type/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "搜尋引擎"
}
}
],
"should": [
{
"match": {
"content": "訊息佇列"
}
}
],
"must_not": [
{
"match": {
"creator": "Alibaba"
}
}
],
"minimum_should_match": 1
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
step4
沒有must或者filter查詢表示或
GET /technology/type/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"creator": "Lucene"
}
},
{
"match": {
"title": "RocketMQ"
}
}
]
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.3962393,
"hits" : [
{
"_index" : "technology",
"_type" : "type",
"_id" : "3",
"_score" : 1.3962393,
"_source" : {
"title" : "RocketMQ",
"content" : "訊息佇列 RocketMQ 版(原ONS)是阿里雲基於 Apache RocketMQ構建的低延遲、高併發、高可用、高可靠的分散式訊息中介軟體。",
"creator" : "Alibaba",
"cost" : 30
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "1",
"_score" : 0.4700036,
"_source" : {
"title" : "ES搜尋引擎",
"content" : "Elasticsearch是一個基於Lucene的搜尋伺服器。",
"creator" : "Lucene",
"cost" : 60
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "2",
"_score" : 0.4700036,
"_source" : {
"title" : "Solr搜尋引擎",
"content" : "Solr是一個高效能,採用Java開發,基於Lucene的全文搜尋伺服器",
"creator" : "Lucene",
"cost" : 20
}
}
]
}
}
step5
GET /technology/type/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"creator": "Lucene"
}
},
{
"match": {
"title": "RocketMQ"
}
}
],
"minimum_should_match": 2
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}