[英]Elasticsearch: combine match_phrase and match in order to get only results for match_phrase (if any)
我有一個書索引,用於存儲書的全文內容(刪除了停用詞,但這對我的問題並不重要)。 我有以下查詢:
> GET /books/_search
> {
> "_source": {
> "includes": ["author", "title"]
> },
> "query": {
> "bool": {
> "should": [
> {
> "match_phrase": {
> "body": "all happy families are alike"
> }
> },
> {
> "match": {
> "body": "all happy families are alike"
> }
> }
> ]
> }
> }
> }
我得到所有具有最高分數的完整字符串的文檔的匹配項,然后得到具有一個或多個匹配項的較低得分的所有文檔的匹配項:第一個匹配項是得分很高的“ Anna Karenina”,然后是任何具有“高興”字的書,其中的“家庭”。 我想獲得什么:
我很難找到如何獲得第一點。
完全匹配和部分匹配不能有條件地返回。 您可以使用命名查詢在客戶端檢查匹配是否完全/部分匹配。
GET books/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"body": {
"query": "all happy families are alike",
"_name":"exact_match" ---> name of query(can be anything)
}
}
},
{
"match": {
"body": {
"query": "all happy families are alike",
"_name":"partial_match"
}
}
}
]
}
}
}
結果:
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "4i0MeG0BCVIM-bi3Fif1",
"_score" : 4.1589947,
"_source" : {
"title" : "Anna Karenina",
"body" : "all happy families are alike"
},
"matched_queries" : [ ---> returns name of queries where condition matched
"exact_match",
"partial_match"
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "4y0MeG0BCVIM-bi3aScM",
"_score" : 0.44216567,
"_source" : {
"title" : "book 1",
"body" : "happy alike"
},
"matched_queries" : [ ---> returns name of queries where condition matched
"partial_match"
]
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.