demo
base
#
# 最开始,type被类比成数据库的table,但是lucene底层对于同一index下面相同的field(type不同)必须是要同种类型,即lucene底层会把index下所有相同的field一致对待。
# 多个type不利于数据压缩
# 全文检索中用于计分的词条统计会更精准
#
PUT my_index
{
"mappings": {
"properties": {
"title": { "type": "text" },
"name": { "type": "text" },
"age": { "type": "integer" },
"created": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
}
PUT users
{
"settings": {
"index.mapping.single_type": true
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
}
}
PUT tweets
{
"settings": {
"index.mapping.single_type": true
},
"mappings": {
"_doc": {
"properties": {
"content": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"tweeted_at": {
"type": "date"
}
}
}
}
}
POST _reindex
{
"source": {
"index": "twitter",
"type": "user"
},
"dest": {
"index": "users"
}
}
POST _reindex
{
"source": {
"index": "twitter",
"type": "tweet"
},
"dest": {
"index": "tweets"
}
}
DELETE new_twitter
PUT new_twitter
{
"mappings": {
"_doc": {
"properties": {
"type": {
"type": "keyword"
},
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"content": {
"type": "text"
},
"tweeted_at": {
"type": "date"
}
}
}
}
}
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
},
"script": {
"source": """
ctx._source.type = ctx._type;
ctx._id = ctx._type + '-' + ctx._id;
ctx._type = '_doc';
"""
}
}
PUT index?include_type_name=false
{
"mappings": {
"properties": {
"foo": {
"type": "keyword"
}
}
}
}
PUT index/_mappings?include_type_name=false
{
"properties": {
"bar": {
"type": "text"
}
}
}
GET index/_mappings?include_type_name=false
PUT index/_doc/1
{
"foo": "baz"
}
GET index/_doc/1
POST index/_update/1
{
"doc": {
"foo": "qux"
}
}
# can not format. if you do, you get errors when you execut
POST _bulk
{"index":{"_index":"index","_id":"3"}}
{"foo":"baz"}
{"index":{"_index":"index","_id":"4"}}
{"foo":"qux"}
field alias
- field alias 并不是真是存在于 document source 中。
- Currently only the search and field capabilities APIs will accept and resolve field aliases.
PUT trips
{
"mappings": {
"properties": {
"distance": {
"type": "long"
},
"route_length_miles": {
"type": "alias",
"path": "distance"
},
"transit_mode": {
"type": "keyword"
}
}
}
}
GET _search
{
"query": {
"range" : {
"route_length_miles" : {
"gte" : 39
}
}
}
}
PUT trips
{
"mappings": {
"properties": {
"distance": {
"type": "long"
},
"route_length_miles": {
"type": "alias",
"path": "distance"
},
"transit_mode": {
"type": "keyword"
}
}
}
}
GET _search
{
"query": {
"range" : {
"route_length_miles" : {
"gte" : 39
}
}
}
}
array
PUT my_index/_doc/1
{
"message": "some arrays in this document...",
"tags": [ "elasticsearch", "wow" ],
"lists": [
{
"name": "prog_list",
"description": "programming list"
},
{
"name": "cool_list",
"description": "cool stuff list"
}
]
}
PUT my_index/_doc/2
{
"message": "no arrays in this document...",
"tags": "elasticsearch",
"lists": {
"name": "prog_list",
"description": "programming list"
}
}
GET my_index/_search
{
"query": {
"match": {
"tags": "elasticsearch"
}
}
}
text/binary/ip/geo/boolean/date/date_nanos
- The binary type accepts a binary value as a Base64 encoded string.
- The binary type accepts a binary value as a Base64 encoded string.
- date
- format : “2015-01-01” or “2015/01/01 12:10:30” or 1420070400001
- date range query on long represention
- date stored as a string depending on the date format that is associated with the field
- date_nanos: Aggregations are still on millisecond resolution, even when using a date_nanos field.
DELETE mapping_index
PUT mapping_index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword":{
"type":"keyword"
}
}
},
"ip_addr":{
"type": "ip"
},
"location":{
"type": "geo_point"
},
"is_published":{
"type": "boolean"
},
"blob": {
"type": "binary"
},
"created":{
"type": "date"
},
"created_in_nanos":{
"type": "date_nanos"
}
}
}
}
# can not format. if you do, you get errors when you execut
POST _bulk
{"index":{"_index":"mapping_index","_id":"1"}}
{"name":"Some binary blob","ip_addr":"192.168.1.1","is_published":true,"location":{"lat":41.12,"lon":-71.34},"blob":"U29tZSBiaW5hcnkgYmxvYg==","created":"2019-07-21","created_in_nanos":"2019-07-21"}
{"index":{"_index":"mapping_index","_id":"2"}}
{"name":"zhengcj","ip_addr":"192.168.1.2","is_published":true,"location":"41.12,-71.34","blob":"U29tZSBiaW5hcnkgYmxvYg==","created":"2019-07-30T16:51:30Z","created_in_nanos":"2019-07-30T16:51:30Z"}
{"index":{"_index":"mapping_index","_id":"3"}}
{"name":"dongyc","ip_addr":"192.168.1.3","is_published":false,"location":"drm3btev3e86","blob":"U29tZSBiaW5hcnkgYmxvYg==","created":1420070400001,"created_in_nanos":1420070400}
# milliseconds-since-the-epoch
PUT mapping_index/_doc/4
{
"name": "zhengjerry",
"ip_addr":"192.168.1.4",
"is_published":false,
"location": [ -71.34, 41.12 ],
"blob": "U29tZSBiaW5hcnkgYmxvYg==",
"created":1420070400001,
"created_in_nanos":1420070400
}
GET mapping_index/_search
{
"query": {
"term": {
"ip_addr": {
"value": "192.168.0.0/16"
}
}
}
}
GET mapping_index/_search
{
"size": 0,
"aggs": {
"publish_state": {
"terms": {
"field": "is_published"
}
}
},
"script_fields": {
"is_published": {
"script": {
"lang": "painless",
"source": "doc['is_published'].value"
}
}
}
}
GET mapping_index/_search
{
"query": {
"geo_bounding_box":{
"location":{
"top_left":{
"lat":42,
"lon":-72
},
"bottom_right":{
"lat":40,
"lon":-74
}
}
}
}
}
range/integer_range/date_range/ip_range
PUT range_index
{
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"expected_attendees": {
"type": "integer_range"
},
"time_frame": {
"type": "date_range",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
PUT range_index/_doc/1?refresh
{
"expected_attendees" : {
"gte" : 10,
"lte" : 20
},
"time_frame" : {
"gte" : "2015-10-31 12:00:00",
"lte" : "2015-11-01"
}
}
GET range_index/_search
{
"query" : {
"term" : {
"expected_attendees" : {
"value": 12
}
}
}
}
GET range_index/_search
{
"query" : {
"range" : {
"time_frame" : {
"gte" : "2015-10-31",
"lte" : "2015-11-01",
"relation" : "within"
}
}
}
}
PUT range_index/_mapping
{
"properties": {
"ip_whitelist": {
"type": "ip_range"
}
}
}
PUT range_index/_doc/2
{
"ip_whitelist" : "192.168.0.0/16"
}
GET range_index/_mapping
token_count
DELETE token_count_index
PUT token_count_index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"length": {
"type": "token_count",
"analyzer": "standard"
}
}
}
}
}
}
PUT token_count_index/_doc/1
{ "name": "John Smith" }
PUT token_count_index/_doc/2
{ "name": "Rachel Alice Williams" }
GET token_count_index/_search
{
"query": {
"term": {
"name.length": 3
}
}
}