ES maping 学习笔记

本文详细介绍了Elasticsearch的Mapping,包括字段类型如String、Numeric、Date等,Meta-fields如_all、_source,以及Mapping参数如Properties、Index、Store等,强调了不同参数在索引、搜索和存储中的作用和配置选项。
摘要由CSDN通过智能技术生成

Filed type(字段类型)

1. 核心数据类型:

String datatype

string

Numeric datatypes

long, integer, short, byte, double, float

Date datatype

date

默认format:"strict_date_optional_time||epoch_millis"是或的关系

strict_date_optional_time 参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#strict-date-time

epoch_millis:milliseconds-since-the-epoch   如:"2015-01-01T12:10:30Z" 还可以是yyyy-MM-dd,yyyy-MM-dd mm:ss等等格式

例子:

PUT my_index{

  "mappings": {

    "my_type": {

      "properties": {

        "date": {

          "type":   "date",

          "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"

        }

      }

    }

  }}

 

 

Boolean datatype

boolean

接受json 值true或是false

false值:false,“false”,”off”,”no”,”0”,””,0,0.0

true值:除了false值以外的值 如1,true.......

Binary datatype

binary

只是一个Base64编码的字符串  this field is not stored by default,and not searchable

2.复杂数据类型

Array datatype

数组不需要是一个专门的类型


Arry 可以为空值null_value或者是直接跳过,

 


 

Object datatype

为单一json对象

 

Nested datatype

nested 为json对象数组


3.Geo 数据类型:

Geo-point datatype

geo_point for lat/lon points 接受经纬度对(latitude-longitude pairs)

在一个边界框内,离具体点的具体距离,多边形内,找geo_point

根据文档里中心节点的物理位置或是距离,聚集(aggregation)文档

根据距离对文档(document)排序

将距离整合为关联性得分(relevance score)

Geo-Shape datatype

geo_shape for complex shapes like polygons

4. 特殊数据类型


IPv4 datatype

ip for IPv4 addresses

Completion datatype

completion to provide auto-complete suggestions

Token count datatype

token_count to count the number of tokens in a string

mapper-murmur3

murmur3 to compute hashes of values at index-time and store them in the index

Attachment datatype

See the mapper-attachments plugin which supports indexing attachments like Microsoft Office formats, Open Document formats, ePub, HTML, etc. into an attachment datatype.

 

二 Meta-fields

1、_all:主要指的是All Field字段,我们可以将一个或都多个包含进不,在进行检索时无需指定字段的情况下检索多个字段。前提是你得开启All Field字段

"_all" : {"enabled" : true}

2、_source

主要指的是Source Field字段Source可以理解为Es除了将数据保存在索引文件中,另外还有一分源数据。_source字段我在们进行检索时相当重要,如果在{"enabled" : false}情况下默认检索只会返回ID,你需通过Fields字段去倒索索引中去取数据,当然效率不是很高。如果觉得enabale:true时,索引的膨涨率比较大的情况下可以通过下面一些辅助设置进行优化:

不被indexed,该字段不能被search,被stored

Compress:是否进行压缩,建议一般情况下将其设为true 

"includes" : ["author", "name"],

"excludes" : ["sex"]

上面的includes和 excludes主要是针对默认情况下面_source一般是保存全部Bulk过去的数据,我们可以通过include,excludes在字段级别上做出一些限索。

3._index

4._type

5._id

_id值可用于query(),但是不能用于aggregationscript,或是sorting,这些情况应该用_uid代替

PUT my_index/my_type/1{

  "text": "Document with ID 1"}

 

PUT my_index/my_type/2{

  "text": "Document with ID 2"}

 

GET my_index/_search{

  "query": {

    "terms": {

      "_id": [ "1", "2" ] 

    }

  }}

 

6._uid _type_id两个域的值构成,用#分隔

7._all

ElasticSearch默认为每个被索引的文档都定义了一个特殊的域 - '_all',它自动包含被索引文档中一个或者多个域中的内容,在进行搜索时,如果不指明要搜索的文档的域,ElasticSearch则会去搜索_all域。_all带来搜索方便,其代价是增加了系统在索引阶段对CPU和存储空间资源的开销。

 默认情况,ElasticSarch自动使用_all所有的文档的域都会被加到_all中进行索引。可以使用"_all" : {"enabled":false} 开关禁用它。如果某个域不希望被加到_all中,可以使用"include_in_all":false

例如:

1.{  
2.   "person": {  
3.      "_all": { "enabled": true }  
4.      "properties": {  
5.         "name": {  
6.            "type": "object",  
7.            "dynamic": false,  
8.            "properties": {  
9.               "first": {  
10.                  "type": "string",  
11.                  "store": true,  
12.                  "include_in_all": false  
13.               },  
14.               "last": {  
15.                  "type": "string",  
16.                  "index": "not_analyzed"  
17.               }  
18.            }  
19.         },  
20.         "address": {  
21.            "type": "object",  
22.            "include_in_all": false,  
23.            "properties": {  
24.               "first": {  
25.                  "properties": {  
26.                     "location": {  
27.                        "type": "string",  
28.                        "store": true,  
29.                        "index_name": "firstLocation"  
30.                     }  
31.                  }  
32.               },  
33.               "last": {  
34.                  "properties": {  
35.                     "location": {  
36.                        "type": "string"  
37.                     }  
38.                  }  
39.               }  
40.            }  
41.         },  
42.         "simple1": {  
43.            "type": "long",  
44.            "include_in_all": true  
45.         },  
46.         "simple2": {  
47.            "type": "long",  
48.            "include_in_all": false     #---simple2 字段不包含在_all
49.         }  
50.      }  
51.   }  
52.}  

查询时,_all和其它域一样使用:

1. GET /profiles/_search  
2. {  
3.     "query": {  
4.         "match": {  
5.            "_all": "food"  
6.         }  
7.     }  
8. }  


 

或者在不提供搜索域的情况下,默认会搜索_all,例如:

1. GET /profiles/_search  
2. {  
3.     "query": {  
4.         "query_string": {  
5.             "query": "food"  
6.         }  
7.     }  
8. }  


head插件的混合查询中  选GET会查询所有document信息,query不起作用,用query要选POST


8._field_names

用来索引document中包含除了null以外其他任何值的字段名字

这个字段可用到existsmissing query中,用以查找document中含有或是不含non-null值得特殊字段

PUT my_index/my_type/1{

  "title": "This is a document"}

PUT my_index/my_type/1{

  "title": "This is another document",

  "body": "This document has a body"}

GET my_index/_search{

  "query": {

    "terms": {

      "_field_names": [ "title" ] 

    }},

  "aggs": {

    "Field names": {

      "terms": {

        "field": "_field_names", 

        "size": 10

      }   }

  },

  "script_fields": {

    "Field names": {

      "script": "doc['_field_names']" 

    }

  }}

VIEW IN SENSE 

Querying on the _field_names field (also see the exists and missing queries)

Aggregating on the _field_names field

Accessing the _field_names field in scripts (inline scripts must be enabled for this example to work)

 

9._meta

10._parent

11._routing

12._t

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值