我有一个书籍索引,其中存储书籍的全文内容(删除了停用词,但这对我的问题并不重要)。 我有以下疑问:
> 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"
> }
> }
> ]
> }
> }
> }
我得到了所有具有最高分数的完整字符串的文档的匹配项,然后,具有较低分数的那些具有一个或多个匹配术语的文档:第一个匹配是具有非常高分数的“安娜·卡列尼娜”,然后是任何具有其中有“幸福”、“家庭”。 我想获得什么:
- 如果文档与条件“match_phrase”匹配,则仅获取此内容 结果(即只得到安娜·卡列尼娜,丢弃其余的)
- 否则,按降序列出所有匹配文档(预期行为)
我很难找到如何获得第 1 点。
最佳答案
无法有条件地返回完全匹配和部分匹配。 您可以使用named queries在客户端检查匹配是否完全/部分。
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"
]
}
]
}
关于Elasticsearch:将 match_phrase 和 match 结合起来,以便仅获取 match_phrase 的结果(如果有),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921752/