索引操作
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 human
DELETE human
DELETE test_object_nested
PUT 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/_mapping
POST 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
# 查看指定索引的Mapping
GET book/_mapping?pretty
# 新增文档新字段
POST book/_doc/2
{
"country": "China"
}
DELETE book
# 设置索引Mapping
PUT book
{
"mappings": {
"dynamic": "false",
"properties": {
"country": {
"type": "keyword"
}
}
}
}
# 查看指定索引的Mapping
GET 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
# 设置索引Mapping
PUT book
{
"mappings": {
"dynamic": "strict",
"properties": {
"country": {
"type": "keyword"
}
}
}
}
# 查看指定索引的Mapping
GET 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
# 设置索引Mapping
PUT book
{
"mappings": {
"dynamic": "strict",
"properties": {
"country": {
"type": "keyword"
}
}
}
}
# 新增Mapping里面有的字段
POST book/_doc/1
{
"country": "china"
}
# 查看指定索引的Mapping
GET book/_mapping?pretty
# 查看文档
GET book/_search
GET book_alias/_search
POST _aliases
{
"actions": [
{
"add": {
"index": "book",
"alias": "book_alias"
}
}
]
}
# 别名列表
GET _cat/aliases?v
文档操作
DELETE book
# 设置索引Mapping
PUT book
{
"mappings": {
"dynamic": "false",
"properties": {
"country": {
"type": "keyword"
}
}
}
}
# 查看指定索引的Mapping
GET 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 book
GET book
# 设置索引Mapping
PUT book
{
"mappings": {
"dynamic": "false",
"properties": {
"nameText": {
"type": "text",
"analyzer": "standard"
},
"nameKeyWord": {
"type": "keyword"
},
"country": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
# 查看指定索引的Mapping
GET 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"
}
}
}
# 带有 operator
GET book/_search
{
"query": {
"match": {
"nameText": {
"query": "what is",
"operator": "and"
}
}
}
}
# match_phrase
GET book/_search
{
"query": {
"match_phrase": {
"nameText": {
"query": "in action"
, "slop": 0
}
}
}
}
# match_phrase_prefix
GET 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 Query
GET book/_search
{
"query": {
"range": {
"price": {
"gte": 400,
"lte": 500
}
}
}
}
# Prefix Query
GET book/_search
{
"query": {
"prefix": {
"nameKeyWord": "ES in a"
}
}
}
# constant_score 转成 filtering
GET book/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"country": "China"
}
}
}
}
}
# bool query
GET 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"
}
}
}
}