gpt4 book ai didi

elasticsearch - Elasticsearch显示不应显示的结果

转载 作者:行者123 更新时间:2023-12-02 23:07:16 27 4
gpt4 key购买 nike

我有一个 flex 搜索索引与此映射。

{
"hotels": {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "text"
},
"star": {
"type": "float"
}
}
}
}
}

在索引里面,我放了一些文件。其中一份文件是这样的。
{
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
然后,我在名称和/或位置字段中搜索匹配的文档。结果应在仅匹配特定单词的单词的顶部显示匹配短语。因此,我创建了此查询。
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
},
{
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
}
查询结果显示了我上面提供的示例数据。我期望的是 Queens Hotel将不会显示在结果中,因为在查询中存在与文档不匹配的 loremipsum。如何实现呢?

最佳答案

The query result shown the example data that I provided above.


这是因为您在搜索查询中使用的bool with应该子句,其作用类似于逻辑OR运算符。在您的搜索查询中,should子句中包含两个多重匹配查询。即使满足多重匹配查询中给出的任何条件,文档也将返回。
分别考虑多重匹配查询,
{
"query": {
"bool": {
"should": {
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
}
}
}
}
没有搜索结果
考虑第二个多重比对查询,
{
"query": {
"bool": {
"should": {
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "1",
"_score": 0.10536051,
"_source": {
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
}
]
因此,由于第二个多重匹配查询而出现了结果,您在其中添加了type: most_fields甚至"fuzziness": "AUTO"
添加另一个索引数据,其确切的酒店名称为 queens loremipsum
{
"name": "queens loremipsum",
"location": "West Australia",
"star": 3.5
}
搜索查询:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
},
{
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
}
搜索结果:
这两个文档都匹配(因为should子句),但是由于在第一个类型为multi_matchphrase查询中,因此应用了boost,因此该文档的得分高于其他文档。
"hits": [
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "2",
"_score": 9.630155, <-- note this
"_source": {
"name": "queens loremipsum",
"location": "West Australia",
"star": 3.5
}
},
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "1",
"_score": 0.18232156, <-- note this
"_source": {
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
}
]
注意:如果只希望匹配确切的文档,则将搜索查询修改为:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
}
]
}
}
}
更新1:
尝试以下搜索查询,您可以根据使用情况更改模糊性参数
     {
"query": {
"bool": {
"should": [
{
"bool": {
"must": {
"multi_match": {
"query": "queens australia",
"type": "cross_fields", <-- note this
"operator": "and",
"fields": [
"name",
"location"
],
"boost": 10
}
}
}
},
{
"bool": {
"must": [
{
"multi_match": {
"query": "queens",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
},
{
"multi_match": {
"query": "australia",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
]
}
}
}

关于elasticsearch - Elasticsearch显示不应显示的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64339433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com