2016-08-24 104 views
3

我是新来的弹性搜索,所以我挣扎了一下,为我们的数据找到最佳查询。弹性搜索查询同时使用match_phrase_prefix和模糊性?

想象一下,我想匹配下列单词“Handelsstandens Boldklub”。

目前,我使用下面的查询:

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

目前,它列出如果我搜索“手”这个词,但如果我搜索“处理”一词将不再上市因为我做了一个错字。但是,如果我以“Handlesstandens”结尾,它会再次列出,因为模糊会碰到输入错误,但只有当我输入整个单词时。

它是否有可能在同一时间做phrase_prefix和模糊?那么在上面的例子中,如果我在路上犯了一个错字,它还会列出这个单词吗?

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

还有什么其他解决方法可以实现上述体验?我喜欢phrase_prefix匹配,因为它也支持马虎匹配(因此我可以搜索“Boldklub汉”,它会列出结果)

或者可以通过使用完成建议程序来实现上述目标吗?

回答

0

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

这是一个非常好的解释它的功能和工作原理。 https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch

这里是我使用的设置和映射:(这是elasticsearch护栏语法)

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

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

我希望有人发现它有帮助,因为我个人在模糊性而不是ngram方面想的很多(之前不知道)。这导致我走错了方向。