是否可以进行nGram模糊搜索?

时间:2017-07-25 14:41:56

标签: elasticsearch

我试图让nGram过滤器使用模糊搜索,但它不会。具体来说,我试图得到" rugh"匹配"粗略"。

我不知道它是否不可能,或者有可能但是我已经定义了错误的映射,或者映射没问题但我的搜索没有正确定义

映射:

{
    settings = new
        {
            index = new
            {
                number_of_shards = 1,
                number_of_replicas = 1,

                analysis = new
                {
                    filter = new
                    {
                        edge_ngram_filter = new
                        {
                            type = "nGram",
                            min_gram = 3,
                            max_gram = 8
                        }
                    }, // filter

                    analyzer = new
                    {
                        analyzer_ngram = new
                        {
                            type = "custom",
                            tokenizer = "standard",
                            filter = new string[]
                            {
                                "lowercase",
                                "edge_ngram_filter"
                            }
                        }
                    }  // analyzer

                } // analysis

            } // index
        },  //  settings

    mappings = new
    {
        j_cv = new
        {
            properties = new
            {
                Text = new
                {
                    type = "text",
                    include_in_all = false,
                    analyzer = "analyzer_ngram",
                    search_analyzer = "standard"
                }
            }
        }   //  j_cv
    }   //  mappings
}

文件:

{
   Id = Guid.NewGuid(),
   Name = "Jimmy Riddle",
   Keyword = new List<string>(new string[] { "Hunting", "High", "Hotel", "California" }),
   Text = "Rough Justice was a program on BBC some years ago. It was quite interesting. Will this match?"
}

搜索:

{
    query = new
    {
        query_string = new
        {
            fields = new string[] { "Text" },
            fuzziness = "3",
            query = "rugh"
        }
    }
}

顺便说一下,&#34;呃&#34;确实匹配你所期望的。

感谢您提供任何帮助,

亚当。

1 个答案:

答案 0 :(得分:1)

通常应该在索引和搜索时应用相同的分析器,因此search_analyzer=standard是错误的,如果删除它应该可以正常工作。 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html

编辑: 你忘记了模糊算子&#34;〜&#34;在您的查询中,如果您将其添加到&#34; rugh&#34;它会起作用!