Elasticsearch数字字段的部分匹配 [英] Elasticsearch partial matching of a number field

查看:81
本文介绍了Elasticsearch数字字段的部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试部分匹配数字字段.在下面的查询中,我想要id(定义为long)匹配任何以419开头的文档.(因此4191应该匹配,419534应该匹配,但123419不匹配)

I'm trying to partially match a number field. In the query below, I'd like id (which is defined as a long) to match any document which starts with 419. (so 4191 should match, as should 419534 but not 123419)

{
    "size": 20,
    "from": 0,
    "sort": [{
        "customerName": "asc"
    }],
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "term": {
                            "id": 419
                        }
                    }]
                }
            }]
        }
    }
}

有人能在我的查询中使用整洁的解决方案吗?

Anyone got a neat solution to use in my query?

推荐答案

要避免出现eggram ngram,可以在id映射中声明一个未分析的文本子字段:

To avoid a egde ngram, you could declare a not analyzed text sub field in your id mapping :

"mappings": {
    "default": {
        "properties": {
            "id": {
                "type": "integer",
                "fields": {
                    "prefixed": {
                        "type":  "string",
                        "index":    "not_analyzed"
                    }
                }
            },
            ...
        }
    }
}

并使用前缀查询针对该字段:

"query": {
    "prefix" : { 
        "id.prefixed" :  { "value" : 419 } 
    }
}

这篇关于Elasticsearch数字字段的部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆