【问题标题】:Elastic search query using match_phrase_prefix and fuzziness at the same time?同时使用 match_phrase_prefix 和模糊性的弹性搜索查询?
【发布时间】:2016-12-31 08:19:34
【问题描述】:

我是弹性搜索的新手,所以我在为我们的数据找到最佳查询时有些吃力。

假设我想匹配以下单词“Handelsstandens Boldklub”。

目前,我正在使用以下查询:

{
    query: {
      bool: {
        should: [
          {
            match: {
              name: {
                query: query, slop: 5, type: "phrase_prefix"
              }
            }
          },
          {
            match: {
              name: {
                query: query,
                fuzziness: "AUTO",
                operator: "and"
              }
            }
          }
        ]
      }
    }
  }

如果我搜索“Hand”,它当前会列出该词,但如果我搜索“Handle”,则该词将不再列出,因为我输入了一个错字。但是,如果我以“Handlesstandens”结束,它将再次列出,因为模糊会捕捉到错字,但只有当我输入整个单词时。

是否有可能同时进行短语前缀和模糊性?那么在上述情况下,如果我在途中打错字,它仍然会列出单词吗?

所以在这种情况下,如果我搜索“Handle”,它仍然会匹配“Handelsstandens Boldklub”这个词。

或者还有什么其他变通方法可以实现上述体验?我喜欢短语前缀匹配,因为它也支持草率匹配(因此我可以搜索“Boldklub han”,它会列出结果)

或者以上可以通过使用补全提示器来实现吗?

【问题讨论】:

    标签: elasticsearch autocomplete fuzzy-search match-phrase


    【解决方案1】:

    好的,所以在进一步研究了 elasticsearch 之后,我得出了我应该使用 ngrams 的结论。

    这里很好地解释了它的作用和工作原理。 https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch

    这是我使用的设置和映射:(这是 elasticsearch-rails 语法)

    settings analysis: {
      filter: {
        ngram_filter: {
          type: "ngram",
          min_gram: "2",
          max_gram: "20"
        }
      },
      analyzer: {
        ngram_analyzer: {
          type: "custom",
          tokenizer: "standard",
          filter: ["lowercase", "ngram_filter"]
        }
      }
    } do
      mappings do
        indexes :name, type: "string", analyzer: "ngram_analyzer"
        indexes :country_id, type: "integer"
      end
    end
    

    还有查询:(这个查询实际上是同时在两个不同的索引中搜索)

    {
        query: {
          bool: {
            should: [
              {
                bool: {
                  must: [
                    { match: { "club.country_id": country.id } },
                    { match: { name: query } }
                  ]
                }
              },
              {
                bool: {
                  must: [
                    { match: { country_id: country.id } },
                    { match: { name: query } }
                  ]
                }
              }
            ],
            minimum_should_match: 1
          }
        }
      }
    

    但基本上你应该只做一个匹配或多匹配查询,这取决于你要搜索多少个字段。

    我希望有人觉得它有帮助,因为我个人在模糊性而不是 ngrams 方面思考了很多(以前不知道)。这把我引向了错误的方向。

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 1970-01-01
      • 2020-06-15
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多