ElasticSearch - 使用连字符进行名称搜索

6
我有一个产品目录,我正在使用Elastica客户端将它索引到ElasticSearch中。顺便说一下,我刚接触ElasticSearch。

我的目录中有一些产品名称中带有“t-shirt”。但是,如果我输入“tshirt”,它们将不会出现在搜索结果中。

我该怎么做才能让“t-shirt”也出现在结果中呢?

我已经按照这个教程为索引实现了以下功能:

'analysis' => array(
    'analyzer' => array(
        'indexAnalyzer' => array(
            'type' => 'custom',
            'tokenizer' => 'whitespace',
            'filter' => array('lowercase', 'mySnowball')
        ),
        'searchAnalyzer' => array(
            'type' => 'custom',
            'tokenizer' => 'whitespace',
            'filter' => array('lowercase', 'mySnowball')
        )
    ),
    'filter' => array(
        'mySnowball' => array(
            'type' => 'snowball',
            'language' => 'English'
        )
    )
)
1个回答

6
你可以尝试使用映射字符过滤器来去除连字符:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-mapping-charfilter.html

类似这样的代码会移除连字符:

{
    "index" : {
        "analysis" : {
            "char_filter" : {
                "my_mapping" : {
                    "type" : "mapping",
                    "mappings" : ["-=>"]
                }
            },
            "analyzer" : {
                "custom_with_char_filter" : {
                    "tokenizer" : "standard",
                    "char_filter" : ["my_mapping"]
                }
            }
        }
    }
}

它有点粗糙,因为它会去掉所有连字符,但它应该能够使“t-shirt”和“tshirt”匹配。

如果您想使用simple_query_string查询,请不要忘记添加"analyze_wildcard":true",如果您想使用通配符。否则,您将无法搜索t-, t-s等内容。 - Roeland Van Heddegem
2
如果用户搜索“T恤”, - Julien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接