Elasticsearch Mapping字段未支持索引导致搜索失效问题处理

Posted on 2022-12-01 18:02  FLGB  阅读(525)  评论(0编辑  收藏  举报

问题描述:

生产上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"
            }
        }
    ]
}

查询索引别名

POST /_aliases
{
    "actions": [
        {
            "add": {
                "index": "indexName_new",
                "alias": "indexName"
            }
        }
    ]
}

 

(2)删除indexName,然后根据上述步骤重新建立indexName,然后reindex indexName_new数据到indexName,最后删除indexName_new即可

1、删除原有indexName索引:

DELETE indexName
2、新增indexName
PUT indexName
{
    "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_new"
  },
  "dest": {
    "index": "indexName"
  }
}
4、查询reindex后的索引,确认数据没问题
GET indexName/_search { "query": { "match_all": {} } }

也可以根据查询总条数等多种方式校验

5、删除indexName_new
DELETE indexName_new

 

Copyright © 2024 FLGB
Powered by .NET 9.0 on Kubernetes