问题描述:
生产上Es根据一个时间字段搜索,却没有返回数据
问题分析:
根据命令:GET indexName/_mapping 查看
#GET indexName/ _mapping
{
"indexName": {
"mappings": {
"properties": {
"channel_source": {
"type": "integer"
},
"content_id": {
"type": "integer"
},
"count_name": {
"type": "integer",
**"index": false**
},
"count_type": {
"type": "integer"
},
"insert_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"pv": {
"type": "integer"
}
}
}
}
}
count_name设置了 “index”: false导致根据该字段搜索导致索引不生效。
ES的mappings定义好了生成索引后是不支持修改现有的字段的,只能新增属性。
解决方案
使用reindex命令处理
1、运行命令:GET indexName 拿到索引的mappings和settings
(不要遗漏了settings属性)
{
"indexName" : {
"aliases" : { },
"mappings" : {
"properties" : {
"channel_source" : {
"type" : "integer"
},
"content_id" : {
"type" : "integer"
},
"count_name" : {
"type" : "integer",
"index" : false
},
"count_type" : {
"type" : "integer"
},
"insert_time" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss"
},
"pv" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"refresh_interval" : "1s",
"number_of_shards" : "3",
"translog" : {
"flush_threshold_size" : "1024m",
"sync_interval" : "120s",
"durability" : "async"
},
"provided_name" : "indexName",
"max_result_window" : "10000",
"creation_date" : "1648546177107",
"number_of_replicas" : "3",
"uuid" : "SkIOphVHQhq9hLbkxrQgwQ",
"version" : {
"created" : "7010099"
}
}
}
}
}
2、根据PUT命令创建indexName_new
注意需要去除对应字段下的(“index” : false)
PUT indexName_new
{
"mappings" : {
"properties" : {
"channel_source" : {
"type" : "integer"
},
"content_id" : {
"type" : "integer"
},
"count_name" : {
"type" : "integer"
},
"count_type" : {
"type" : "integer"
},
"insert_time" : {
"type" : "date"
},
"pv" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"refresh_interval" : "1s",
"number_of_shards" : "3",
"translog" : {
"flush_threshold_size" : "1024m",
"sync_interval" : "120s",
"durability" : "async"
}
}
}
}
3、创建成功后,执行reindex命令
POST _reindex
{
"source": {
"index": "indexName"
},
"dest": {
"index": "indexName_new"
}
}
4、查询一下indexName_new对应字段的搜索是否好了
GET /indexName_new/_search
{
"query": {
"match_all": {}
}
}
5、设置索引别名或索引再次替换都可以
(1)设置该索引(indexName_new)别名为indexName
PUT indexName_new/_alias/indexName
或
POST /_aliases
{
"actions": [
{
"add": {
"index": "indexName_new",
"alias": "indexName"
}
}
]
}
查询索引别名
GET indexName_new/_alias
(2)删除indexName,然后根据上述步骤重新建立indexName,然后reindex** indexName_new数据到indexName,最后删除indexName_new即可**文章来源:https://www.toymoban.com/news/detail-671029.html
删除索引命令:文章来源地址https://www.toymoban.com/news/detail-671029.html
DELETE indexName_new
到了这里,关于Elasticsearch Mapping字段未支持索引导致搜索失效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!