Elastic Search the Custom Mapping

Mapping is here that the index structure, you can be seen as a database table structure, including the field names, field types, inverted index-related settings. es query index mapping structure as the api endpoint _mapping, as shown below:

The right of the search results we called index of "mapping". Es then look at how to create a custom mapping, api as follows:


books Field datatypes (Field Type)

es supported data types Elasticsearch of indexing and document spoke of a text, and then add a few here:

  • String Type: text or keyword
  • Value Type: integer, long, short, byte, double, float, etc.
  • Boolean type: boolean
  • Date Type: date
  • Binary type: binary
  • Range Type: integer_range, double_range, date_range, float_range
  • Array Type: array
  • Object type: object
  • Nested types: nested object
  • Location data types: geo_point, geo_shape
  • Private Type: ip, join, token count, percolator, etc.

Which text and keyword types, text types of fields will be automatically word when adding or editing a document, such as the following full_name fields in the new document data when the "James Bond" word in inverted index is "James, state Germany "; and address the field, regardless of the word, is what the original text into the text. Obviously, keyword types than text to save space, the writing efficiency is high, and therefore in a custom mapping (ie the index structure) must be appropriate, whether the field needs a good word.


books Mapping parameters (mapping parameters supported)

1、analyzer:字段的分词器,默认是standard,分词及分词器在这篇文章中有详细讲:elasticsearch之分词

2、boost:用户查询时,提高字段的相关性算分,意思就是,比如搜索"中国"关键字,但是两个文档里都含有单词"中国",那么返回时谁排在第一位呢?这里有个相关性算分的过程,得分越高自然就排在前面,这里通过boost参数来显式的指定权重,默认boost是1.0,要注意的是boost设置的值在范围查询的相关性算分中不会起作用。

3、copy_to:该属性允许将多个字段的值copy到指定字段,然后可以将其作为单个字段查询,例如下面的first_name和last_name字段复制到full_name字段

4、dynamic:是否允许新增字段,例如某索引只有两个字段,但是你新增文档时插入了三个字段。可选值:true(默认)表示允许自动新增字段,false表示不允许自动新增字段,但是文档可以正常写入,但无法对字段进行查询等操作,strict表示文档不能写入,报错。

5、index:控制字段是否索引,默认为true,false表示不记录索引,也就不可搜索。

6、index_options:控制倒排索引记录的内容,es的倒排索引介绍移步这里:elasticsearch之倒排索引。可选项:

  • docs:只记录文档id
  • freqs:记录文档id、单词频率
  • positions:记录文档id、词频、单词位置
  • offsets:记录文档id、词频、单词位置、偏移量

其中text类型字段默认的index_options为positions,其余类型默认为docs,同时记录的内容越多,占用的空间也越大。

7、fields:允许你为字段设置子字段,可以有多个,比如检索人的中文姓名和拼音姓名,把name_pinyin这个字段挂在name_cn字段下

8、null_value:当字段遇到null值时候的处理策略(字段为null时候是不能被搜索的,也就是说,text类型的字段不能使用该属性),设置该值后可以用你设置的值替换null值,这点可类比mysql中的"default"设置默认值

9、search_analyzer:指定搜索时分词器,这一要注意,在 elasticsearch之分词 中说到过,分词的两个时机是索引时分词和搜索时分词,一般情况下使用索引时分词即可,所以如果你同时设置了两个,那么这两个分词器最好保持一致,不然可能出现搜索匹配不到数据的问题。


books 其它建议

在es中,当索引不存在时候,你直接新增数据es会自动创建相适应的mapping,es可以自动识别文档字段类型,极大降低了用户的使用成本。这点和mysql中的表不一样,mysql必须要先建表结构,然后才能新增数据,但es中会自动创建及识别字段类型。由这个引出了如何创建自定义mapping的问题,可以采取如下建议:

1、写入一条文档到es的索引中,获取es为这个索引自动生动的mapping

2、通过endpoint为_mapping的查看索引的mapping的api,并修改这个mapping,加入自定义配置

3、创建实际所需的索引


books 上一篇: Elastic search之分词及分词器

books 下一篇: Elastic search之Search Api(Query DSL)、字段查询、复合查询

books  参考:es官方文档https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

发布了202 篇原创文章 · 获赞 571 · 访问量 147万+

Guess you like

Origin blog.csdn.net/fanrenxiang/article/details/85317344