微信登录

索引库 - 索引库的操作 - 增加字段

索引映射更新:只能增加索引字段

  1. PUT /索引库名/_mapping
  2. {
  3. "properties":{
  4. 新字段名":{
  5. "type": "integer"
  6. }
  7. }
  8. }

properties 特性
索引库和mapping一旦创建无法修改,但是可以添加新的字段,语法如下:

  1. PUT /索引库名/_mapping
  2. {
  3. "properties":{
  4. "新字段名":{type": "integer"}
  5. }
  6. }
  1. POST test/test/_mapping
  2. {
  3. "test": {
  4. "_all": {
  5. "analyzer": "ik_max_word",
  6. "search_analyzer": "ik_max_word",
  7. "term_vector": "no",
  8. "store": "false"
  9. },
  10. "properties": {
  11. "content": {
  12. "type": "string",
  13. "store": "no",
  14. "term_vector": "with_positions_offsets",
  15. "analyzer": "ik_max_word",
  16. "search_analyzer": "ik_max_word",
  17. "include_in_all": "true",
  18. "boost": 8
  19. }
  20. }
  21. }
  22. }

上面的命令,是定义test索引下test类型的映射。其中定义了_all字段的分析方法,以及content属性的分析方法。

这里介绍下什么是_all字段,其实_all字段是为了在不知道搜索哪个字段时,使用的。es会把所有的字段(除非你手动设置成false),都放在_all中,然后通过分词器去解析。当你使用query_string的时候,默认就在这个_all字段上去做查询,而不需要挨个字段遍历,节省了时间。

字段 含义 释义
properties 定义了特定字段的分析方式 在上面的例子中,仅仅设置了content的分析方法。
type 字段的类型为string 只有string类型才涉及到分词,像是数字之类的是不需要分词的。
store 定义字段的存储方式 no代表不单独存储,查询的时候会从_source中解析。
store 定义字段的存储方式 当你频繁的针对某个字段查询时,可以考虑设置成true。
term_vector 定义了词的存储方式,with_position_offsets,意思是存储词语的偏移位置,在结果高亮的时候有用。
analyzer 定义了索引时的分词方法
search_analyzer 定义了搜索时的分词方法
include_in_all 定义了是否包含在_all字段中
boost 跟计算分值相关的
索引库 - 索引库的操作 - 增加字段