首页 > 其他分享 >Elasticsearch Mapping字段未支持索引导致搜索失效问题处理

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

时间:2022-12-01 18:11:55浏览次数:40  
标签:count index indexName Mapping 字段 Elasticsearch new integer type

问题描述:

生产上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

 

标签:count,index,indexName,Mapping,字段,Elasticsearch,new,integer,type
From: https://www.cnblogs.com/flgb/p/16942240.html

相关文章

  • ElasticSearch面试题
    1.为什么要使用ElasticSearch系统中的数据,随着业务的发展,时间的推移,将会非常多,而业务中往往采用模糊查询进行数据的搜索,而模糊查询会导致查询引擎放弃索引,导致系统......
  • Linux搭建ElasticSearch集群
    前言这是整个ElasticSearch搭建的最后一篇文章,其实对我而言ElasticSearch在Linux上搭建集群写这篇文章意义并不大,只是为了补充这个空白而已,所以这篇文章并不会讲解很详细......
  • ElasticSearch集群数据读写流程
    前言本章作为ElasticSearch分布式集群的附属章节,主要讲解ElasticSearch集群环境下数据是如何读写的,既然讲到读写,那么ElasticSearch的更新就是基于二者的结合,顺带也讲一下......
  • ELasticSearch优化
    硬件优化Elasticsearch的基础是Lucene,所有的素引和文档数据是存储在本地的磁盘中,具体的路径可在ES的配置文件./config/elasticsearch.yml中配置,如下:磁盘在现代服务......
  • ElasticSearch分布式集群
    前言关于ElasticSearch集群概念这里就不多废话了,详细可见ElasticSearch基本介绍、ElasticSearch集群系统架构单节点集群我们可以创建一个索引,为这个索引创建三个分片......
  • SpringBoot整合ElasticSearch-SpringData
    前言之前写过一篇SpringBoot整合ElasticSearch是使用的elasticsearch-rest-high-level-client,这篇文章使用Spring-Data来操作ElasticSearch。关于ElasticSearch的搭建我......
  • ElasticSearch集群概念
    单机存在的问题单台Elasticsearch服务器提供服务,往往都有最大的负载能力,超过这个阈值,服务器性能就会大大降低甚至不可用,所以生产环境中,一般都是运行在指定服务器集......
  • Windows安装ElasticSearch
    前言习惯使用docker安装各种中间件了,但是程序包安装方式也不能丢呀。官网下载地址,我这里使用的是7.4.2,如果需要使用其他版本,更改连接后面的版本号即可!下载下载速度还......
  • ElasticSearch集群系统架构
    前言全面几篇文章主要是使用单机跑ElasticSearch的,在生产环境为了保证高可用和高吞吐量我们都会采用集群的方式部署。那么本章不涉及ElasticSearch集群的搭建,只涉及理论......
  • Windows搭建ElasticSearch集群
    前言在搭建ElasticSearch集群前,可以先看看往期文章Windows安装ElasticSearch,可以使用上篇文章中下载ElasticSearch搭建准备将下载好的ElasticSearch复制三分,node1为......