首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹性搜索模糊查询意外结果

弹性搜索模糊查询意外结果
EN

Stack Overflow用户
提问于 2020-03-03 01:04:31
回答 1查看 116关注 0票数 0

我有两个指数,城市和地方。一个地方有这样的地图:

代码语言:javascript
复制
{
    "mappings": {
        "properties": {
            "cityId": {
                "type": "integer"
            },
            "cityName": {
                "type": "text"
            },
            "placeName": {
                "type": "text"
            },
            "status": {
                "type": "keyword"
            },
            "category": {
                "type": "keyword"
            },
            "reviews": {
                "properties": {
                    "rating": {
                        "type": "long"
                    },
                    "comment": {
                        "type": "keyword"
                    },
                    "user": {
                        "type": "nested"
                    }
                }
            }
        }
    }
}

City索引是这样映射的:

代码语言:javascript
复制
{
    "mappings": {
        "properties": {
            "state": {
                "type": "keyword"
            },
            "postal": {
                "type": "keyword"
            },
            "phone": {
                "type": "keyword"
            },
            "email": {
                "type": "keyword"
            },
            "notes": {
                "type": "keyword"
            },
            "status": {
                "type": "keyword"
            },
            "cityName": {
                "type": "text"
            },
            "website": {
                "type": "keyword"
            },
            "cityId": {
                "type": "integer"
            }
        }
    }
}

最初,我们只有一个文档,其中城市有嵌入的地方,但我在搜索嵌套位置数组时遇到了困难,因此我将结构更改为这样,我希望能够在一个具有模糊性的查询中同时搜索cityName和placeName。我有一个城市,它的名字里有焊工这个词,在同一位置的一些地方,他们的名字里也有焊工这个词,其中有一个类型:文本。但是,当搜索焊工时,以下两个查询(见下文)都不返回这些文档,搜索焊工或焊工确实会返回这些文档。我不知道为什么焊工不能与焊工的*相匹配。在创建这两个索引期间,我没有指定任何分析器,我也没有在查询中显式地定义它,任何人都可以帮助我完成这个查询,因此它的行为与预期的一样:

查询1: index = places

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                }

            ]
        }
    }
}

查询2: index = places

代码语言:javascript
复制
{
    "query": {
        "match": {
            "placeName": {
                "query": "welder",
                "fuzziness": 20
            }
        }
    }
}

有人会发帖询问,当通过一个词时,焊工会返回在他们名下有焊工的文件(也应该适用于其他术语,这只是一个例子)。

编辑1 : --这是一个示例place文档,我希望通过上面发布的任何查询返回:

代码语言:javascript
复制
{
   cityId: 29,
   placeName: "Welder's Garage Islamabad",
   cityName: "Islamabad",
   status: "verified",
   category: null,
   reviews: []
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-11 03:55:00

使用您的映射、查询和模糊设置为"20“,我将得到文档。模糊性: 20将容忍20编辑之间的搜索词和焊工之间的距离,所以甚至"w“将匹配”焊工的“。我认为这个值在实际查询中是不同的。

如果要搜索焊工或焊工,并返回焊工的,则可以使用stemmer令牌滤波器

制图:

代码语言:javascript
复制
PUT indexfuzzy
{
  "mappings": {
    "properties": {
      "cityId": {
        "type": "integer"
      },
      "cityName": {
        "type": "text"
      },
      "placeName": {
        "type": "text",
        "analyzer": "my_analyzer"
      },
      "status": {
        "type": "keyword"
      },
      "category": {
        "type": "keyword"
      },
      "reviews": {
        "properties": {
          "rating": {
            "type": "long"
          },
          "comment": {
            "type": "keyword"
          },
          "user": {
            "type": "nested"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "stem_possessive_english",
            "stem_minimal_english"
          ]
        }
      },
      "filter": {
        "stem_possessive_english": {
          "type": "stemmer",
          "name": "possessive_english"
        },
        "stem_minimal_english": {
          "type": "stemmer",
          "name": "minimal_english"
        }
      }
    }
  }
}

查询:

代码语言:javascript
复制
GET indexfuzzy/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                           "query": "welder"--> welder,welders,welder's will work
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder"
                        }
                    }
                }

            ]
        }
    }
}

结果:

代码语言:javascript
复制
[
      {
        "_index" : "indexfuzzy",
        "_type" : "_doc",
        "_id" : "Jc-yx3ABd7NBn_0GTBdp",
        "_score" : 0.2876821,
        "_source" : {
          "cityId" : 29,
          "placeName" : "Welder's Garage Islamabad",
          "cityName" : "Islamabad",
          "status" : "verified",
          "category" : null,
          "reviews" : [ ]
        }
      }
    ]

所有格英语:-从记号中移除尾随s

代码语言:javascript
复制
GET <index_name>/_analyze
{
  "text": "Welder's Garage Islamabad",
  "analyzer": "my_analyzer"
}

返回

代码语言:javascript
复制
{
  "tokens" : [
    {
      "token" : "welder", --> will be matched for welder's, welders
      "start_offset" : 0,
      "end_offset" : 8,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "garage",
      "start_offset" : 9,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "islamabad",
      "start_offset" : 16,
      "end_offset" : 25,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60498692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档