索引操作
PUT human{ "settings":{ "number_of_shards":1, "number_of_replicas":1 }, "mappings":{ "dynamic":"false", "properties":{ "blackFlag":{ "type":"byte" }, "brandName":{ "type":"keyword", "index":false }, "keyWord":{ "type":"text", "analyzer":"ik_max_word" }, "onlineDate":{ "type":"long" }, "unitPrice":{ "type":"scaled_float", "scaling_factor":100 }, "publicAttrList":{ "type":"nested", "dynamic":"false", "properties":{ "publicAttrCode":{ "type":"keyword" }, "publicAttrName":{ "type":"keyword", "index":false }, "publicAttrValue":{ "type":"keyword" }, "publicAttrValueCode":{ "type":"keyword" }, "publicAttrValueName":{ "type":"keyword", "index":false } } } } }}GET humanDELETE human
DELETE test_object_nestedPUT test_object_nested{ "mappings": { "dynamic": "false", "properties": { "name_object": { "type": "object", "properties": { "first": { "type": "keyword" }, "last": { "type": "keyword" } } }, "name_nested": { "type": "nested", "properties": { "first": { "type": "keyword" }, "last": { "type": "keyword" } } } } }}GET test_object_nested/_mappingPOST test_object_nested/_doc/1{ "name_object":[ { "first" : "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ], "name_nested":[ { "first" : "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ]}GET test_object_nested/_search# 对象数组查询GET test_object_nested/_search{ "query": { "bool": { "must": [ { "term": { "name_object.first": { "value": "John" } } }, { "term": { "name_object.last": { "value": "White" } } } ] } }}# 嵌套对象数组查询GET test_object_nested/_search{ "query": { "bool": { "must": [ { "nested": { "path": "name_nested", "query": { "bool": { "must": [ { "term": { "name_nested.first": { "value": "John" } } }, { "term": { "name_nested.last": { "value": "White" } } } ] } } } } ] } }}
DELETE book# 索引添加文档POST book/_doc/1{ "name": "ES in aciton", "price": 11.22, "stockFlag": true, "count": 12, "author": { "name": "some gay", "age": 30 }, "labelList": ["ES", "IT"], "date": null}# 查看文档GET book/_search# 查看指定索引的MappingGET book/_mapping?pretty# 新增文档新字段POST book/_doc/2{ "country": "China"}
DELETE book# 设置索引MappingPUT book{ "mappings": { "dynamic": "false", "properties": { "country": { "type": "keyword" } } }}# 查看指定索引的MappingGET book/_mapping?pretty# 索引添加文档POST book/_doc/1{ "name": "ES in aciton", "type": "IT", "country": "china"}# 查看文档GET book/_search# 搜索Mapping有的字段GET book/_search?q=country:china# 搜索Mapping没有的字段GET book/_search?q=type:IT# 新增Mapping里面没有的字段POST book/_doc/2{ "age": 110}# 搜索Mapping没有的字段GET book/_search?q=age:110
DELETE book# 设置索引MappingPUT book{ "mappings": { "dynamic": "strict", "properties": { "country": { "type": "keyword" } } }}# 查看指定索引的MappingGET book/_mapping?pretty# 新增Mapping里面有的字段POST book/_doc/1{ "country": "china"}# 查看文档GET book/_search# 索引添加文档POST book/_doc/1{ "name": "ES in aciton", "type": "IT", "country": "china"}# 搜索Mapping有的字段GET book/_search?q=country:china
DELETE book# 设置索引MappingPUT book{ "mappings": { "dynamic": "strict", "properties": { "country": { "type": "keyword" } } }}# 新增Mapping里面有的字段POST book/_doc/1{ "country": "china"}# 查看指定索引的MappingGET book/_mapping?pretty# 查看文档GET book/_searchGET book_alias/_searchPOST _aliases{ "actions": [ { "add": { "index": "book", "alias": "book_alias" } } ]}# 别名列表GET _cat/aliases?v
文档操作
DELETE book# 设置索引MappingPUT book{ "mappings": { "dynamic": "false", "properties": { "country": { "type": "keyword" } } }}# 查看指定索引的MappingGET book/_mapping?pretty# 指定 IP 创建文档# 再次请求就是覆盖更新POST book/_doc/1{ "name": "ES in aciton", "type": "IT", "country": "china"}# 不指定 ID 创建,ES会随机分配ID# 再次请求就继续创建,不幂等POST book/_doc{ "name": "ES in aciton", "type": "IT", "country": "china"}# 查看文档GET book/_search# 部分更新POST book/_update/1{ "doc": { "name": "Lucene", "age": 12 }}# 使内外部版本号,_seq_no 与 _primary_term# 可确一个文档的版本号,两者要跟文档一致PUT book/_doc/1?if_seq_no=16&if_primary_term=1{ "name": "ES in aciton1", "type": "IT", "country": "china"}# 使用外部版本号,一定要比内部version 大PUT book/_doc/1?version=19&version_type=external{ "name": "ES in aciton1", "type": "IT", "country": "china"}# 根据 id 查找GET book/_doc/1
搜索与聚合
# 删除指定索引DELETE bookGET book# 设置索引MappingPUT book{ "mappings": { "dynamic": "false", "properties": { "nameText": { "type": "text", "analyzer": "standard" }, "nameKeyWord": { "type": "keyword" }, "country": { "type": "keyword" }, "price": { "type": "float" } } }}# 查看指定索引的MappingGET book/_mapping?pretty# 索引添加文档POST book/_bulk{"create" : {"_id": 1}}{ "nameText": "ES in action ES","nameKeyWord": "ES in action ES","country": "China","price":100}{"create" : {"_id": 2}}{ "nameText": "ES in one action","nameKeyWord": "ES in one action","country": "Japan","price":200}{"create" : {"_id": 3}}{ "nameText": "ES in one two action","nameKeyWord": "ES in one two action","country": "America","price":300}{"create" : {"_id": 4}}{ "nameText": "What is ES","nameKeyWord": "What is ES","country": "China","price":400}{"create" : {"_id": 5}}{ "nameText": "one thing is good","nameKeyWord": "one thing is good","country": "America","price":500}# 查看文档GET book/_search# 查看指定索引指定字段的分词效果GET book/_analyze{ "field": "nameText", //"field": "nameKeyWord", "text": "china"}# 全文检索GET book/_search{ "query": { "match": { "nameText": "ES" } }}# 带有 operatorGET book/_search{ "query": { "match": { "nameText": { "query": "what is", "operator": "and" } } }}# match_phraseGET book/_search{ "query": { "match_phrase": { "nameText": { "query": "in action" , "slop": 0 } } }}# match_phrase_prefixGET book/_search{ "query": { "match_phrase_prefix": { "nameText": { "query": "in a", "slop": 0 } } }}# 精确检索GET book/_search{ "query": { "term": { "country": "china" } }}# 精确检索GET book/_search{ "query": { "term": { "nameKeyWord": "ES in one two" } }}# Range QueryGET book/_search{ "query": { "range": { "price": { "gte": 400, "lte": 500 } } }}# Prefix QueryGET book/_search{ "query": { "prefix": { "nameKeyWord": "ES in a" } }}# constant_score 转成 filteringGET book/_search{ "query": { "constant_score": { "filter": { "term": { "country": "China" } } } }}# bool queryGET book/_search{ "query": { "bool": { "must": { "match": { "nameText": "ES" } }, "must_not": { "match": { "nameText": "two" } }, "filter": { "range": { "price": { "gte": 300 } } }, "should": [ { "term": { "country": "China" } }, { "term": { "country": "America" } } ], "minimum_should_match": 1 } }}# Bucket 聚合GET book/_search{ "size": 0, "aggs": { "country_aggs": { "terms": { "field": "country" } } }}# Metric 聚合GET book/_search{ "size": 0, "aggs": { "average_price": { "avg": { "field": "price" } }, "max_price": { "max": { "field": "price" } }, "min_price": { "min": { "field": "price" } } }}