ElasticSearch - Searching with hyphens in name

半腔热情 提交于 2019-11-28 03:57:17

问题


I have a product catalog which I am indexing in ElasticSearch using the Elastica client. I am very new to ElasticSearch BTW.

There are products in my catalog which have 't-shirt' in their names. But, they won't appear in search results if I type 'tshirt'.

What can I do so that 't-shirt' can also pop-up in results?

I have followed this tutorial and implemented the following for indexes:

'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:


You can try removing the hyphen using a mapping char filter:

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

something like this would remove the hyphen:

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

it's something of a blunt force instrument as it will strip all hyphens but it should make "t-shirt" and "tshirt" match



来源:https://stackoverflow.com/questions/23637493/elasticsearch-searching-with-hyphens-in-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!