问题描述:

生产上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 拿到索引的mappingssettings

(不要遗漏了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即可**

删除索引命令:

DELETE indexName_new

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐