elasticsearch 弹性搜索-筛选与值和数据类型匹配的数据
我正在使用AWS的ES 5.7。弹性搜索集群中存储有对象列表。每个对象都有一个字段状态值为整数,但由于indexer的一个编程错误,某些对象的状态值存储为文本而不是整数。我需要使用bool查询过滤状态为text的对象 我使用下面的查询来过滤数据 样本数据elasticsearch 弹性搜索-筛选与值和数据类型匹配的数据,elasticsearch,types,aws-elasticsearch,elasticsearch,Types,Aws Elasticsearch,我正在使用AWS的ES 5.7。弹性搜索集群中存储有对象列表。每个对象都有一个字段状态值为整数,但由于indexer的一个编程错误,某些对象的状态值存储为文本而不是整数。我需要使用bool查询过滤状态为text的对象 我使用下面的查询来过滤数据 样本数据 { "took":22, "timed_out":false, "_shards":{ "total":16, "successful":16, "failed":0 },
{
"took":22,
"timed_out":false,
"_shards":{
"total":16,
"successful":16,
"failed":0
},
"hits":{
"total":3208,
"max_score":1,
"hits":[
{
"_index":"entity-data",
"_type":"account",
"_id":"b7b46c",
"_score":1,
"_source":{
"status":"3"
}
},
{
"_index":"entity-data",
"_type":"account",
"_id":"b7b46",
"_score":1,
"_source":{
"status":3
}
}
]
}
}
用于根据状态进行筛选的布尔查询
{
"query":{
"bool":{
"filter":[
{
"term":{
"status": "3"
}
}
]
}
}
}
Here "status": "3" and "status": 3 is providing same result.
我需要过滤“状态”为“3”的数据
任何建议都会有帮助。提前感谢。您的脚本不起作用,因为字段的映射类型为
long
,在使用您编写的脚本进行搜索时,它只查看long类型的反向索引
您可以使用无痛脚本访问文档值并查找所有字符串值。脚本检查字段的数据类型状态
,并仅对字符串类型返回true。因此,它将返回包含字符串值的所有文档
PUT t1/doc/1
{
"status": 3
}
PUT t1/doc/2
{
"status": "3"
}
GET t1/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"inline": "if(params._source.status instanceof String) return true;",
"lang": "painless"
}
}
}
}
}
}
输出:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "t1",
"_type": "doc",
"_id": "2",
"_score": 0,
"_source": {
"status": "3"
}
}
]
}
}
其他信息:
如果要将所有这些字符串值更改为long,可以将索引重新编制为新索引,并使用脚本操作这些值
//Create new index
PUT t2
//reindex from t1 to t2 and change string to integer
POST _reindex
{
"source": {
"index": "t1"
},
"dest": {
"index": "t2"
},
"script": {
"lang": "painless",
"inline": "if(ctx._source.status instanceof String){ctx._source.status = Integer.parseInt(ctx._source.status)}"
}
}
由于字段的映射类型为long
,因此脚本不起作用,在使用您编写的脚本进行搜索时,它只查看long类型的倒排索引
您可以使用无痛脚本访问文档值并查找所有字符串值。脚本检查字段的数据类型状态
,并仅对字符串类型返回true。因此,它将返回包含字符串值的所有文档
PUT t1/doc/1
{
"status": 3
}
PUT t1/doc/2
{
"status": "3"
}
GET t1/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"inline": "if(params._source.status instanceof String) return true;",
"lang": "painless"
}
}
}
}
}
}
输出:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "t1",
"_type": "doc",
"_id": "2",
"_score": 0,
"_source": {
"status": "3"
}
}
]
}
}
其他信息:
如果要将所有这些字符串值更改为long,可以将索引重新编制为新索引,并使用脚本操作这些值
//Create new index
PUT t2
//reindex from t1 to t2 and change string to integer
POST _reindex
{
"source": {
"index": "t1"
},
"dest": {
"index": "t2"
},
"script": {
"lang": "painless",
"inline": "if(ctx._source.status instanceof String){ctx._source.status = Integer.parseInt(ctx._source.status)}"
}
}
您还可以提供此ES索引的映射吗?您可以通过执行get entity data
来获取它。您还可以提供此ES索引的映射吗?您可以通过执行get entity data