英文:
Elasticsearch: match_phrase_prefix can't find items when search query ends with '0'
问题
在我的Elastic中,我有这个条目:AB-001-123-B
当运行一个带有match_phrase_prefix查询的查询时,像这样:
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"code": "AB-00"
}
}
]
}
}
我没有得到任何结果。
当将代码更改为
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"code": "AB-001"
}
}
]
}
}
它返回了该条目。当从代码中删除-00
时,它也返回该条目。
我进行了几次其他条目的测试。似乎无法查询以0结尾的搜索短语。
为什么会这样?有没有办法修复这个查询?我尝试过转义,但没有任何效果。
英文:
In my Elastic i'm having this entry: AB-001-123-B
When running a query with match_phrase_prefix like this:
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"code": "AB-00"
}
}
]
}
},
...
I don't get any results.
When changing the code to
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"code": "AB-001"
}
}
]
}
},
...
It returns the entry. When removing -00
from the code it returns the entry as well.
I did several tests with other entries. It seems like he is not able to query when the search-phrase ends with 0.
Why is that? Is there a way to fix this for the query? I tried escaping, without any effects.
答案1
得分: 1
code
字段是一个由standard
分析器分析的text
字段。这意味着AB-001-123-B
会被分析为以下标记:
GET _analyze
{
"analyzer": "standard",
"text": "AB-001-123-B"
}
响应 =>
{
"tokens" : [
{
"token" : "ab",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "001",
"start_offset" : 3,
"end_offset" : 6,
"type" : "<NUM>",
"position" : 1
},
{
"token" : "123",
"start_offset" : 7,
"end_offset" : 10,
"type" : "<NUM>",
"position" : 2
},
{
"token" : "b",
"start_offset" : 11,
"end_offset" : 12,
"type" : "<ALPHANUM>",
"position" : 3
}
]
}
match_phrase_prefix
对于您的用例不太理想。更好的方法是使用prefix
查询来查询code.keys
字段,像这样:
"query": {
"bool": {
"should": [
{
"prefix": {
"code.keys": "AB-001"
}
}
]
}
}
英文:
The code
field is a text
field analyzed by the standard
analyzer. This means that AB-001-123-B
is analyzed into the following tokens:
GET _analyze
{
"analyzer": "standard",
"text": "AB-001-123-B"
}
Response =>
{
"tokens" : [
{
"token" : "ab",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "001",
"start_offset" : 3,
"end_offset" : 6,
"type" : "<NUM>",
"position" : 1
},
{
"token" : "123",
"start_offset" : 7,
"end_offset" : 10,
"type" : "<NUM>",
"position" : 2
},
{
"token" : "b",
"start_offset" : 11,
"end_offset" : 12,
"type" : "<ALPHANUM>",
"position" : 3
}
]
}
The match_phrase_prefix
is not ideal for your use case. It would be better to query the code.keys
field using a prefix
query, like this:
"query": {
"bool": {
"should": [
{
"prefix": {
"code.keys": "AB-001"
}
}
]
}
},
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论