PUT my_index{
"settings": {
"number_of_shards ": 5,
"number_of_replicas": 1
},
"mappings": {
"my_doc": {
"properties": {
"title": {
"type": "text",
"normalizer": "my_normalizer"
},
"name": {
"type": "text",
"analyzer": "standard",
"boost": 2
},
"age": {
"type": "integer"
},
"created": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
}
}
curl写法 (后面类似以下写法,不再列出)
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
# 与上面一致
}
'
1.text 字符串,分词,全文索引
2.keyword 关键字,不分词,适合id,email等这种不分词的字段
3.numeric 数字类型有integer、long、short、byte、double、float等类型
4.date 时间类型
5.boolean 布尔类型
6.binary 接收base64编码的字符串
7.rang 具体有integer_range,float_range,long_range,double_range,date_range,ip_range,可存储范围数据,如下插入
PUT index/type/id
{
"field_name" : {
"gte" : 10,
"lte" : 20
}
}
8.数组类型,es实际上不存在array类型,es每个类型都支持转成数组类型,也就是不管定义成integer还是text等都可以以数组形式存进去,如果需要存integer数组,那只需要将这个字段定义成integer就可以了
9.object 对象类型 json格式
10.nested
嵌套类型,object嵌套数组
11.geo_point
经纬度 可存入对象json,字符串,数组
12.ip
可存ipv4 ipv6地址
13.token_count integet类型,统计词个数
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text",
"fields": {
"length": {
"type": "token_count",
"analyzer": "standard"
}
}
}
}
}
}
}
PUT my_index/_doc/2
{ "name": "Rachel Alice Williams" }
# 查询name字段有三个词的数据
GET my_index/_search
{
"query": {
"term": {
"name.length": 3
}
}
}
14.join
用于在同个索引下创建父子关系的类型
# 定义父子关系 question父 answer子
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
}
}
}
}
}
# 插入父数据
PUT my_index/_doc/1
{
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}
# 插入子数据 routing指向根节点 parent指向直接父节点
PUT my_index/_doc/2?routing=1
{
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
其他数据类型还有Alias,mapper-murmur3,
mapper-annotated-text,
Percolator type,Completion,Geo-Shape datatype
详细参考官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/6.x/mapping-types.html
PUT my_index
{
"settings":{
"analysis":{
"analyzer":{
"my_stop_analyzer":{
"type":"custom",
"tokenizer":"standard",
"filter":[
"lowercase",
"english_stop"
]
}
},
"filter":{
"english_stop":{
"type":"stop",
"stopwords":"_english_"
}
}
}
},
"mappings":{
"_doc":{
"properties":{
"title": {
"type":"text",
"analyzer":"my_analyzer"
}
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
}
}
}
}
}
PUT my_index/_doc/1
{
"first_name": "John",
"last_name": "Smith"
}
GET my_index/_search
{
"query": {
"match": {
"full_name": {
"query": "John Smith",
"operator": "and"
}
}
}
}
查询结果为
"_source": {
"first_name": "John",
"last_name": "Smith"
}
PUT my_index
{
"mappings": {
"_doc": {
"dynamic": false,
"properties": {
"user":{
"type":"text"
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"my_field": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"date": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"number_one": {
"type": "integer",
"ignore_malformed": true
},
"number_two": {
"type": "integer"
}
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"message": {
"type": "keyword",
"ignore_above": 20
}
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"city": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
PUT my_index/_doc/1
{
"city": "New York"
}
GET my_index/_search
{
"query": {
"match": {
"city": "york"
}
},
"sort": {
"city.raw": "asc"
},
"aggs": {
"Cities": {
"terms": {
"field": "city.raw"
}
}
}
}
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL"
}
}
}
}
}
PUT my_index/_doc/1
{
"status_code": null
}
PUT my_index/_doc/2
{
"status_code": []
}
GET my_index/_search
{
"query": {
"term": {
"status_code": "NULL"
}
}
}
PUT my_index/_doc/1
{
"names": [ "John Abraham", "Lincoln Smith"]
}
GET my_index/_search
{
"query": {
"match_phrase": {
"names": {
"query": "Abraham Lincoln"
}
}
}
}
通过此方式修改该字段伪差距
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"names": {
"type": "text",
"position_increment_gap": 0
}
}
}
}
}
修改为0后,便可跨词搜索出来,但是必须和Abraham 和 Lincoln一样是相邻的词
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"manager": {
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
},
"employees": {
"type": "nested",
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
}
}
}
}
}
GET my_index/_search
{
"query": {
"match": {
"_all": "Tom Terry"
}
}
}
GET my_index/_search
{
"query": {
"terms": {
"_field_names": [ "name" ]
}
}
}
GET _search
{
"query": {
"exists": {
"field": "_ignored"
}
}
}
GET my_index/_search
{
"query": {
"terms": {
"_id": [ "1", "2" ]
}
}
}
GET index_1,index_2/_search
{
"query": {
"terms": {
"_index": ["index_1", "index_2"]
}
},
"aggs": {
"indices": {
"terms": {
"field": "_index",
"size": 10
}
}
}
}
PUT my_index
{
"mappings": {
"user": {
"_meta": {
"class": "MyApp::User",
"version": {
"min": "1.0",
"max": "1.3"
}
}
}
}
}
shard_num = hash(_routing) % num_primary_shards
通过以下方式可以指定路由键为user1,插入数据
PUT my_index/_doc/1?routing=user1&refresh=true
{
"title": "This is a document"
}
GET my_index/_doc/1?routing=user1
通过以下方式请求可以指定只在user1和user2路由键相关联的分片上查找
GET my_index/_search?routing=user1,user2
{
"query": {
"match": {
"title": "document"
}
}
}
如果带路由键插入,但是不带路由键查询,会根据id计算分片,查找不到可能会导致遍历所有分片,因此可以在创建mapping时指定必须带路由键操作。
PUT my_index2
{
"mappings": {
"_doc": {
"_routing": {
"required": true
}
}
}
}
PUT tweets
{
"mappings": {
"_doc": {
"_source": {
"enabled": false
}
}
}
}
一般用于配置某些字段不存储数据 配置如下,includes表示存储数据的字段,excludes标识不存储数据的字段。
PUT logs
{
"mappings": {
"_doc": {
"_source": {
"includes": [
"*.count",
"meta.*"
],
"excludes": [
"meta.description",
"meta.other.*"
]
}
}
}
}
PUT logs/_doc/1
{
"requests": {
"count": 10,
"foo": "bar"
},
"meta": {
"name": "Some metric",
"description": "Some metric description",
"other": {
"foo": "one",
"baz": "two"
}
}
}
GET my_index/_search
{
"query": {
"terms": {
"_uid": [ "_doc#1", "_doc#2" ]
}
}