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

时间:2020-03-03 01:04:31

标签: elasticsearch elastic-stack elasticsearch-5 mongoosastic

我有2个索引,城市和地方。地方1具有这样的映射:

{
    "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"
                    }
                }
            }
        }
    }
}

城市索引是这样映射的:

{
    "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,而且比较模糊。我有一个城市,名称中包含单词Welder's,并且同一位置内的某些地方的名称中也包含单词Welder's,其类型为:文本。但是,当搜索焊工时,以下两个查询都不会返回这些文档,搜索焊工或焊工的确会返回这些文档。我不确定为什么焊工不会与Welder的焊工相匹配。我在创建两个索引的过程中都没有指定任何分析器,我也没有在查询中明确定义它,任何人都可以帮助我解决这个查询,以便它按预期运行:

查询1:索引=地方


{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                }

            ]
        }
    }
}

查询2:索引=地方

{
    "query": {
        "match": {
            "placeName": {
                "query": "welder",
                "fuzziness": 20
            }
        }
    }
}

任何人都可以发布一个查询,该查询通过字焊工时将返回其名称中带有Welder的文档(也应适用于诸如此类的其他术语,这只是示例)

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

{
   cityId: 29,
   placeName: "Welder's Garage Islamabad",
   cityName: "Islamabad",
   status: "verified",
   category: null,
   reviews: []
}

1 个答案:

答案 0 :(得分:0)

使用您的映射和查询以及模糊性设置为“ 20”,我正在获取文档。模糊性:20将容忍所搜索单词和焊工之间的20个编辑距离,因此即使“ w”也将与“焊工”匹配。我认为此值在您的实际查询中有所不同。

如果要搜索一个或多个焊工并返回焊工,则可以使用stemmer token filter

映射:

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"
        }
      }
    }
  }
}

查询:

GET indexfuzzy/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                           "query": "welder"--> welder,welders,welder's will work
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder"
                        }
                    }
                }

            ]
        }
    }
}

结果:

[
      {
        "_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" : [ ]
        }
      }
    ]

possessive_english:-从令牌中删除尾随的 minimal_english:-删除复数

GET <index_name>/_analyze
{
  "text": "Welder's Garage Islamabad",
  "analyzer": "my_analyzer"
}

返回

{
  "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
    }
  ]
}