0
点赞
收藏
分享

微信扫一扫

六、使用elasticsearch创建索引

(一)参考资料
  关于索引的所有详细信息,参见 ​ElasticSearch入门 第三篇:索引

  上面的资料是基于elasticsearch2.x,而笔者是基于elasticsearch6.x,这里简单提一下6.x更新的地方:

  • 字段类型 String 没有了,被 text 和 keyword 两种类型代替。
  • text:分词。比如“三国演义”,若被索引为 text 类型,则将会被分词为 “三国”和“演绎”,则可以通过“三国”、“演绎”和“三国演义”查询到该字段。
  • keyword:不分词 。比如“三国演义”,若被索引为 keyword类型,则只能通过“三国演义”查询到该字段, “三国”和“演绎”将查询不到该字段。

  同时我总结一下,平时使用的时候,注意点:

  1. 在全局配置文件 elasticsearch.yml 中,禁用自动创建索引:​​action.auto_create_index:false​
  2. ​index​​​:该属性控制字段是否编入索引被搜索,该属性共有三个有效值:​​analyzed、no和not_analyzed​
  3. ​store​​:指定是否将字段的原始值写入索引,默认值是no
  4. ​analyzer​​:该属性定义用于建立索引和搜索的分析器名称,默认值是全局定义的分析器名称,该属性可以引用在配置结点(settings)中自定义的分析器;

案例一:

"settings":{
"number_of_shards": "6",
"number_of_replicas": "1",
//指定 ik 分词器
"analysis":{
"analyzer":{
"ik":{
"tokenizer":"ik_max_word"
}
}
}
},

案例二:

{  
"settings":{
"index":{
"analysis":{
"analyzer":{
"myanalyzer_name":{
"tokenizer":"standard",
"filter":[
"asciifolding",
"lowercase",
"ourEnglishFilter"
]
}
},
"filter":{
"ourEnglishFilter":{
"type":"kstem"
}
}
}
}
}
}
  1. ​search_analyzer​​:该属性定义的分析器,用于处理发送到特定字段的查询字符串;

备注:analyzer 和 search_analyzer 的区别:
analyzer:这个主要索引时进行分词
search_analyzer:这个主要查询时进行分词

索引配置案例:

PUT blog
{
"settings":{
"number_of_shards":5,
"number_of_replicas":0
},
"mappings":{
"articles":{
"_routing":{
"required":false
},
"_all":{
"enabled":false
},
"_source":{
"enabled":true
},
"dynamic_date_formats":[
"yyyy-MM-dd",
"yyyyMMdd"
],
"dynamic":"false",
"properties":{
"articleid":{
"type":"long",
"store":true,
"index":"not_analyzed",
"doc_values":true,
"ignore_malformed":true,
"include_in_all":true,
"null_value":0,
"precision_step":16
},
"title":{
"type":"string",
"store":true,
"index":"analyzed",
"doc_values":false,
"ignore_above":0,
"include_in_all":true,
"index_options":"positions",
"position_increment_gap":100,
"fields":{
"title":{
"type":"string",
"store":true,
"index":"not_analyzed",
"doc_values":true,
"ignore_above":0,
"include_in_all":false,
"index_options":"docs",
"position_increment_gap":100
}
}
},
"author":{
"type":"string",
"store":true,
"index":"analyzed",
"doc_values":false,
"ignore_above":0,
"include_in_all":true,
"index_options":"positions",
"position_increment_gap":100,
"fields":{
"author":{
"type":"string",
"index":"not_analyzed",
"include_in_all":false,
"doc_values":true
}
}
},
"content":{
"type":"string",
"store":true,
"index":"analyzed",
"doc_values":false,
"ignore_above":0,
"include_in_all":false,
"index_options":"positions",
"position_increment_gap":100
},
"postat":{
"type":"date",
"store":true,
"doc_values":true,
"format":[
"yyyy-MM-dd",
"yyyyMMdd"
],
"index":"not_analyzed",
"ignore_malformed":true,
"include_in_all":true,
"null_value":"2000-01-01",
"precision_step":16
}
}
}
}
}


举报

相关推荐

0 条评论