首页 > 编程语言 > ElasticSearch添加索引代码实例解析
2020
09-25

ElasticSearch添加索引代码实例解析

1. 编写索引内容

  节点解释:

  settings:配置信息

  "number_of_replicas": 0 不需要备份(单节点的ElasticSearch使用)

  "mappings": 映射内容

  "dynamic":false 是否动态索引,这里使用的是false,表示索引的固定的,不需要修改。

  "properties": 属性结构内容

  "index":"true" 需要分词处理的结构

  type对应的数据类型,text文本(长字符串),integer数字,date时间,keyword单词

elasticsearch 6.X版本的索引文件

{
 "settings":{
  "number_of_replicas": 0
 },
 "mappings":{
  "house":{
   "dynamic":false,
   "properties":{
    "houseId":{"type":"long"},
    "title":{"type":"text", "index":"true"},
    "price":{"type":"integer"},
    "area":{"type":"integer"},
    "createTime":{"type":"date","format":"strict_date_optional_time||epoch_millis"},
    "lastUpdateTime":{"type":"date", "format":"strict_date_optional_time||epoch_millis"},
    "cityEnName":{"type":"keyword"},
    "regionEnName":{"type":"keyword"},
    "direction":{"type":"integer"},
    "distanceToSubway":{"type":"integer"},
    "subwayLineName":{"type":"keyword"},
    "subwayStationName":{"type":"keyword"},
    "tags":{"type":"text"},
    "district":{"type":"keyword"},
    "description":{"type":"text", "index":"true"},
    "layoutDesc":{"type":"text", "index":"true"},
    "traffic":{"type":"text", "index":"true"},
  "roundService": {"type": "text", "index": "true"},
    "rentWay":{"type":"integer"}
   }
  }
 }
}

elasticsearch 7.X版本的索引文件

{
 "settings":{
  "number_of_replicas": 0
 },
 "mappings":{
  "dynamic":false,
  "properties":{
   "title":{"type":"text", "index":"true"},
   "price":{"type":"integer"},
   "area":{"type":"integer"},
   "createTime":{"type":"date","format":"strict_date_optional_time||epoch_millis"},
   "lastUpdateTime":{"type":"date", "format":"strict_date_optional_time||epoch_millis"},
   "cityEnName":{"type":"keyword"},
   "regionEnName":{"type":"keyword"},
   "direction":{"type":"integer"},
   "distanceToSubway":{"type":"integer"},
   "subwayLineName":{"type":"keyword"},
   "subwayStationName":{"type":"keyword"},
   "tags":{"type":"text"},
   "district":{"type":"keyword"},
   "description":{"type":"text", "index":"true"},
   "layoutDesc":{"type":"text", "index":"true"},
   "traffic":{"type":"text", "index":"true"},
  "roundService": {"type": "text", "index": "true"},
   "rentWay":{"type":"integer"}
  }
 }
}

2. 创建索引

  使用Postmen发送创建索引请求

  (1)地址栏后半段是索引名称

  (2)请求使用的PUT方式,选择Body,raw形式,采用JSON格式发送

  创建成功的显示结果:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "house"
}

  在ElasticSearch-Head里查看结果:

3. 创建索引时的报错:

  错误1:Root mapping definition has unsupported parameters

  原因:ElasticSearch7.X之后的版本默认不在支持指定索引类型,默认索引类型是_doc(隐含:include_type_name=false),所以在mappings节点后面,直接跟properties就可以了。

  问题2:Could not convert [title.index] to boolean

  原因:也是新版本的问题,之前版本的index属性写法是"analyze",现在只能设置true, false, "true","false"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧