Elasticsearch索引具有这样的json文档
{
id: ABC120379013
name: Matlo Jonipa
jobs: {nested data}
}
定义索引架构时,我将id注册为关键字。
现在,我需要编写一个查询,该查询可以返回id字段值中第4个字符为数字9的所有文档。
# matched docs
id: ABC920379013,
id: Zxr900013000
...
对于这个用例,我有两个问题。
1-将id设置为关键字字段时,是否可以正确索引?我觉得我应该使用一些分析仪,但我不知道是哪个。
2-有人可以指导我如何编写查询以匹配字段的第n个字符吗?
我可以使用此正则表达式匹配第4个字符为9的字符串,但可以在Elasticsearch查询中使用它吗?
/^...9.*$/
or
/^.{3}9.*$/
答案 0 :(得分:1)
Below query would help your use case. I've used Script Query.
Also, yes you are doing it right, you need to make sure that the field id
would be of type keyword
. Note that keyword
type doesn't make use of analyzers
.
POST regexindex/_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "doc['id'].value.indexOf('9') == 3",
"lang": "painless"
}
}
}
}
}
}
I've mentioned .indexOf('9') == 3
because index starts from 0