elasticsearch Mapping、字段类型Field type详解


其中string类型 ELasticsearch 5.X之后的字段类型不再支持string 由text或keyword取代。 如果仍使用string,会给出警告。

字段类型概述
一级分类    二级分类    具体类型
核心类型    字符串类型   text,keyword
整数类型    integer,long,short,byte
浮点类型    double,float,half_float,scaled_float
逻辑类型    boolean
日期类型    date
范围类型    range
二进制类型   binary
复合类型    数组类型    array
对象类型    object
嵌套类型    nested
地理类型    地理坐标类型  geo_point
地理地图    geo_shape
特殊类型    IP类型    ip
范围类型    completion
令牌计数类型  token_count
附件类型    attachment
抽取类型    percolator

put  http://192.168.11.237:9200/tms_site_index/

{
  "mappings": {
    "doc": {
      "properties": {
        "address": {
          "type": "text"
        },
     "province": {
          "type": "text"
        },
    "type": {
          "type": "text"
        }, 
    "lng": {
     "type": "float"
     },
     "lat": {
     "type": "float"
     },
    "id": {
          "type": "long"
         },
        "geo": {
          "type": "geo_point"
        },
    "customer_name": {
          "type": "text"
           "analyzer": "ik_max_word",
           "search_analyzer": "ik_max_word"
        },
    "gmt_created": {
          "type": "date"
        },
    "is_deleted": {
          "type": "integer"
        }
      }
    }
  }
}

customer_name字段 指定分析器 ,先要安装分词器插件

1.2 text类型 text取代了string,当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合(termsAggregation除外)。把full_name字段设为text类型的Mapping如下:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_name": {
          "type":  "text"
        }
      }
    }
  }
}

1.3 keyword类型

keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。

1.4 date类型

JSON中没有日期类型,所以在ELasticsearch中,日期类型可以是以下几种:

日期格式的字符串:e.g. “2015-01-01” or “2015/01/01 12:10:30”. long类型的毫秒数( milliseconds-since-the-epoch) integer的秒数(seconds-since-the-epoch)

{
  "mappings": {
    "my_type": {
      "properties": {
        "date": {
          "type": "date" 
        }
      }
    }
  }
}

1.5 ip类型

ip类型的字段用于存储IPV4或者IPV6的地址

{
  "mappings": {
    "my_type": {
      "properties": {
        "ip_addr": {
          "type": "ip"
        }
      }
    }
  }
}

1.6 token_count类型

token_count用于统计词频: 是否是指,使用次数多的数据?

{
  "mappings": {
    "my_type": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "length": { 
              "type":     "token_count",
              "analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

1.7 geo point 类型

地理位置信息类型用于存储地理位置信息的经纬度: 可以实现附近的人,定位查询等,功能丰富

{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
多种不同的插入方式,如下三种都可行
"location": { 
    "lat": 41.12,
    "lon": -71.34
  }

"location": "41.12,-71.34" 
"location": [ -71.34, 41.12 ]

1.8 _all

_all字段是把其它字段拼接在一起的超级字段,所有的字段用空格分开,_all字段会被解析和索引,但是不存储。当你只想返回包含某个关键字的文档但是不明确地搜某个字段的时候就需要使用_all字段。 使用copy_to自定义_all字段:

GET my_index/_search
{
  "query": {
    "match": {
      "_all": "Java"
    }
  }
}

1.9 _routing

路由参数,ELasticsearch通过以下公式计算文档应该分到哪个分片上:

默认的_routing值是文档的_id或者_parent,通过_routing参数可以设置自定义路由。例如,想把user1发布的博客存储到同一个分片上,索引时指定routing参数,查询时在指定路由上查询

二、Mapping参数

2.1 analyzer

指定分词器(分析器更合理),对索引和查询都有效。如下,指定ik分词的配置:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "content": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}

2.2 normalizer

normalizer用于解析前的标准化配置,比如把所有的字符转化为小写等。例子: 在创建的时候,可以自定义多个分词组合待

PUT index
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "keyword",
          "normalizer": "my_normalizer"
        }
      }
    }
  }
}

2.3 boost

boost字段用于设置字段的权重,比如,关键字出现在title字段的权重是出现在content字段中权重的2倍,设置mapping如下,其中content字段的默认权重是1. 2.4 enabled

ELasticseaech默认会索引所有的字段,enabled设为false的字段,es会跳过字段内容,该字段只能从_source中获取,但是不可搜。而且字段可以是任意类型。

{
  "mappings": {
    "session": {
      "properties": {
        "user_id": {
          "type":  "keyword"
        },
        "last_updated": {
          "type": "date"
        },
        "session_data": { 
          "enabled": false
        }
      }
    }
  }

2.5 format

format属性主要用于格式化日期:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd"
        }
      }
    }
  }
}

更多内置的日期格式:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html 2.6 search_analyzer

大多数情况下索引和搜索的时候应该指定相同的分析器,确保query解析以后和索引中的词项一致。但是有时候也需要指定不同的分析器,例如使用edge_ngram过滤器实现自动补全。

默认情况下查询会使用analyzer属性指定的分析器,但也可以被search_analyzer覆盖。例子:

PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "autocomplete", 
          "search_analyzer": "standard" 
        }
      }
    }
  }
}

四、动态Mapping 4.1 default mapping

在mapping中使用default字段,那么其它字段会自动继承default中的设置。

各种mapping的处理,动态修改可以查询更多内容,高级功能都给整合出来了,没有解决不了的索引问题,只要不了解的知识体系,多熟悉这些才能熟练掌握elasticsearch 查询

感谢原博主的贡献

应该多看看这些功能, 原文参考 https://blog.csdn.net/ZYC88888/article/details/83059040