Elasticsearch部分匹配顺序

时间:2019-01-28 10:59:19

标签: elasticsearch

是否有一种方法可以按字段开头的顺序返回sku,而无需在索引中添加任何其他数据?

搜索查询示例:EXP-0931

返回:NI-EXP-0931作为第一个结果,其次是EXP-0931 / 03 / P

我们希望具有EXP *的产品排在第一位,我试图关闭模糊性,限制为仅sku字段,等等,如果对文档进行了读取也无法使常量查询正常工作。

我的sku字段的映射:

"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256

示例POST请求

{
    "query": {
        "match": {"sku": "EXP"}
    }
}

示例结果

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.5989681,
        "hits": [
            {
                "_index": "magento2_en_catalog_product_20190202_000937",
                "_type": "product",
                "_id": "398925",
                "_score": 0.5989681,
                "_source": {
                    "sku": "NI-EXP-0931"
                }
            },
            {
                "_index": "magento2_en_catalog_product_20190202_000937",
                "_type": "product",
                "_id": "398923",
                "_score": 0.55341274,
                "_source": {
                    "sku": " EXP-0931/03/P"
                }
            }
        ]
    }
}

使用Shingle映射

 "sku": {
                                "type": "text",
                                "fields": {
                                    "untouched": {
                                        "type": "keyword"
                                    }
                                },
                                "copy_to": [
                                    "search",
                                    "spelling"
                                ],
                                "analyzer": "shingle"
                            },

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则您要搜索以 EXP

开头的内容

但是您得到的结果是:NI-EXP-0931,这是不希望的。

我建议您阅读有关tokenizers的内容,以了解您希望如何对术语进行标记……例如,如果您使用Whitespace tokenizer,则搜索词NIC-EXP-0931将仍然是一个单词,但是如果您使用Standard tokenizer,则在相同的期限内您将获得3个令牌(NIC,EXP,0931)。

如果使用空白标记器,则搜索EXP-0931时不应返回NIC-EXP-0931


如果您想进行补全搜索(以 EX 开头的单词),则建议阅读有关令牌过滤器的信息:N-Gram and Edge N-Gram令牌过滤器。他们会告诉Elasticsearch如何为您完成搜索...

请查看this question的一些示例和有关构建完成提示的参考。