Query String Syntax
PUT test_search_index
{
"settings": {
"index":{
"number_of_shards": "1"
}
}
}
POST test_search_index/doc/_bulk
{"index":{"_id":"1"}}
{"username":"alfred way","job":"java engineer","age":18,"birth":"1990-01-02","isMarried":false}
{"index":{"_id":"2"}}
{"username":"alfred","job":"java senior engineer and java specialist","age":28,"birth":"1980-05-07","isMarried":true}
{"index":{"_id":"3"}}
{"username":"lee","job":"java and ruby engineer","age":22,"birth":"1985-08-07","isMarried":false}
{"index":{"_id":"4"}}
{"username":"alfred junior way","job":"ruby engineer","age":23,"birth":"1989-08-07","isMarried":false}
GET test_search_index/_search?q=alfred
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.2039728,
"hits" : [
{
"_index" : "test_search_index",
"_type" : "doc",
"_id" : "2",
"_score" : 1.2039728,
"_source" : {
"username" : "alfred",
"job" : "java senior engineer and java specialist",
"age" : 28,
"birth" : "1980-05-07",
"isMarried" : true
}
},
{
"_index" : "test_search_index",
"_type" : "doc",
"_id" : "1",
"_score" : 0.33698124,
"_source" : {
"username" : "alfred way",
"job" : "java engineer",
"age" : 18,
"birth" : "1990-01-02",
"isMarried" : false
}
},
{
"_index" : "test_search_index",
"_type" : "doc",
"_id" : "4",
"_score" : 0.27601978,
"_source" : {
"username" : "alfred junior way",
"job" : "ruby engineer",
"age" : 23,
"birth" : "1989-08-07",
"isMarried" : false
}
}
]
}
}
GET test_search_index/_search?q=alfred
{
"profile":true
}
GET test_search_index/_search?q=username:alfred
GET test_search_index/_search?q=username:alfred way
{
"profile":true
}
GET test_search_index/_search?q=username:"alfred way"
{
"profile":true
}
GET test_search_index/_search?q=username:(alfred way)
{
"profile":true
}
GET test_search_index/_search?q=username:alfred AND way
{
"profile":true
}
GET test_search_index/_search?q=username:(alfred AND way)
{
"profile":true
}
GET test_search_index/_search?q=username:(alfred NOT way)
GET test_search_index/_search?q=username:(alfred +way)
{
"profile":true
}
GET test_search_index/_search?q=username:(alfred %2Bway)
{
"profile":true
}
GET test_search_index/_search?q=username:alfred age:>26
GET test_search_index/_search?q=username:alfred AND age:>20
GET test_search_index/_search?q=birth:(>1980 AND <1990)
GET test_search_index/_search?q=username:alf*
GET test_search_index/_search?q=username:/[a]?l.*/
GET test_search_index/_search?q=username:alfed
GET test_search_index/_search?q=username:alfed~1
GET test_search_index/_search?q=username:alfd~2
GET test_search_index/_search?q=job:"java engineer"
GET test_search_index/_search?q=job:"java engineer"~1
GET test_search_index/_search?q=job:"java engineer"~2
GET test_search_index/_search
{
"query": {
"match": {
"username": "alfred way"
}
}
}
GET test_search_index/_search
{
"query": {
"match": {
"username": {
"query": "alfred way",
"operator": "and"
}
}
}
}
GET test_search_index/_search
{
"query": {
"match": {
"job": {
"query": "java ruby engineer",
"minimum_should_match": "3"
}
}
}
}
GET test_search_index/_search
{
"explain":true,
"query": {
"match": {
"username": "alfred way"
}
}
}
GET test_search_index/_search
{
"query": {
"match_phrase": {
"job": "java engineer"
}
}
}
GET test_search_index/_search
{
"query": {
"match_phrase": {
"job": "engineer java"
}
}
}
GET test_search_index/_search
{
"query": {
"match_phrase": {
"job": {
"query": "java engineer",
"slop": "2"
}
}
}
}
GET test_search_index/_search
{
"profile":true,
"query":{
"query_string": {
"default_field": "username",
"query": "alfred AND way"
}
}
}
GET test_search_index/_search
{
"profile":true,
"query": {
"query_string": {
"fields": [
"username",
"job"
],
"query": "alfred OR (java AND ruby)"
}
}
}
GET test_search_index/_search
{
"profile":true,
"query":{
"simple_query_string": {
"query": "alfred +way \"java",
"fields": ["username"]
}
}
}
GET test_search_index/_search
{
"query":{
"query_string": {
"default_field": "username",
"query": "alfred +way \"java"
}
}
}
GET test_search_index/_search
{
"query":{
"term":{
"username":"alfred"
}
}
}
GET test_search_index/_search
{
"query":{
"term":{
"username":"alfred way"
}
}
}
G`ET test_search_index/_search
{
"query": {
"terms": {
"username": [
"alfred",
"way"
]
}
}
}
GET test_search_index/_search
{
"query":{
"range": {
"age": {
"gte": 10,
"lte": 30
}
}
}
}
GET test_search_index/_search
{
"query":{
"range": {
"birth": {
"gte": "1980-01-01"
}
}
}
}
GET test_search_index/_search
{
"query":{
"range": {
"birth": {
"gte": "now-30y"
}
}
}
}
GET test_search_index/_search
{
"query":{
"range": {
"birth": {
"gte": "2010||-20y"
}
}
}
}
Constant Score Query
GET test_search_index/_search
{
"query":{
"constant_score": {
"filter": {
"match":{
"username":"alfred"
}
}
}
}
}
GET test_search_index/_search
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"match": {
"job": "java"
}
}
}
},
{
"constant_score": {
"filter": {
"match": {
"job": "ruby"
}
}
}
}
]
}
}
}
Filter
GET test_search_index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"username": "alfred"
}
}
]
}
}
}
Must
GET test_search_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"username": "alfred"
}
},
{
"match": {
"job": "specialist"
}
}
]
}
}
}
Must_Not
GET test_search_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"job": "java"
}
}
],
"must_not": [
{
"match": {
"job": "ruby"
}
}
]
}
}
}
should
GET test_search_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"username": "junior"
}
},
{
"match": {
"job": "ruby"
}
}
]
}
}
}
GET test_search_index/_search
{
"query": {
"bool": {
"should": [
{"term": {"job": "java"}},
{"term": {"job": "ruby"}},
{"term": {"job": "specialist"}}
],
"minimum_should_match": 2
}
}
}
GET test_search_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"username": "alfred"
}
}
],
"should": [
{
"term": {
"job": "ruby"
}
}
]
}
}
}
GET test_search_index/_count
{
"query":{
"match":{
"username": "alfred"
}
}
}
source filtering
GET test_search_index/_search
GET test_search_index/_search?_source=username
GET test_search_index/_search
{
"_source": false
}
GET test_search_index/_search
{
"_source": ["username","age"]
}
GET test_search_index/_search
{
"_source": {
"includes": "*i*",
"excludes": "birth"
}
}
POST test_search_relevance/doc
{
"name":"hello"
}
POST test_search_relevance/doc
{
"name":"hello,world"
}
POST test_search_relevance/doc
{
"name":"hello,world!a beautiful world"
}
GET test_search_relevance/_search
{
"explain": true,
"query": {
"match":{
"name":"hello"
}
}
}
GET test_search_relevance/_search?search_type=dfs_query_then_fetch
{
"query": {
"match":{
"name":"hello"
}
}
}
GET test_search_index/_search
{
"query":{
"match": {
"username": "alfred"
}
},
"sort":{
"birth":"desc"
}
}
GET test_search_index/_search
{
"query":{
"match": {
"username": "alfred"
}
},
"sort": [
{
"birth": "desc"
},
{
"_score": "desc"
},
{
"_doc": "desc"
}
]
}
GET test_search_index/_search
{
"sort":{
"username.keyword":"desc"
}
}
排序
Fielddata
PUT test_search_index/_mapping/doc
{
"properties": {
"job":{
"type":"text",
"fielddata": true
}
}
}
Doc Values
PUT test_doc_values/_mapping/doc
{
"properties": {
"username": {
"type": "keyword",
"doc_values": false
},
"hobby": {
"type": "keyword"
}
}
}
docvalue_fields
GET test_search_index/_search
{
"docvalue_fields": [
"username",
"username.keyword",
"age"
]
}
GET test_search_index/_search
{
"from":0,
"size":2
}
GET test_search_index/_search
{
"from":10000,
"size":2
}
GET test_search_index/_search?scroll=5m
{
"size":1 指明每次scroll返回的文档数
}
POST _search/scroll
{
"scroll" : "5m",
"scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAABswWX3FLSTZFOF9URFdqWHlvX3gtYmhtdw=="
}
PUT test_search_index/doc/10
{
"username":"doc10"
}
过多的scroll调用会占用大量内存,可以用clear api删除过多的scroll快照
DELETE _search/scroll/_all
GET test_search_index/_search
{
"size":1,
"sort":{
"age":"desc",
"_id":"desc"
}
}
GET test_search_index/_search
{
"size":1,
"search_after":[23,"4"],
"sort":{
"age":"desc",
"_id":"desc"
}
}
应用场景
聚合分析
分类
POST test_search_index/doc/_bulk
{"index":{"_id":"1"}}
{"username":"alfred way","job":"java engineer","age":18,"birth":"1990-01-02","isMarried":false,"salary":10000}
{"index":{"_id":"2"}}
{"username":"tom","job":"java senior engineer","age":28,"birth":"1980-05-07","isMarried":true,"salary":30000}
{"index":{"_id":"3"}}
{"username":"lee","job":"ruby engineer","age":22,"birth":"1985-08-07","isMarried":false,"salary":15000}
{"index":{"_id":"4"}}
{"username":"Nick","job":"web engineer","age":23,"birth":"1989-08-07","isMarried":false,"salary":8000}
{"index":{"_id":"5"}}
{"username":"Niko","job":"web engineer","age":18,"birth":"1994-08-07","isMarried":false,"salary":5000}
{"index":{"_id":"6"}}
{"username":"Michell","job":"ruby engineer","age":26,"birth":"1987-08-07","isMarried":false,"salary":12000}
Min
GET test_search_index/_search
{
"size":0,
"aggs":{
"min_age":{
"min": {
"field": "age"
}
}
}
}
Max
GET test_search_index/_search
{
"size":0,
"aggs":{
"max_age":{
"max": {
"field": "age"
}
}
}
}
Avg
GET test_search_index/_search
{
"size":0,
"aggs":{
"avg_age":{
"avg": {
"field": "age"
}
}
}
}
Sum
GET test_search_index/_search
{
"size":0,
"aggs":{
"sum_age":{
"sum": {
"field": "age"
}
}
}
}
GET test_search_index/_search
{
"size": 0,
"aggs": {
"min_age": {
"min": {
"field": "age"
}
},
"max_age": {
"max": {
"field": "age"
}
},
"avg_age": {
"avg": {
"field": "age"
}
},
"sum_age": {
"sum": {
"field": "age"
}
}
}
}
Cardinality
GET test_search_index/_search
{
"size":0,
"aggs":{
"count_of_job":{
"cardinality": {
"field": "job.keyword"
}
}
}
}
Stats
GET test_search_index/_search
{
"size":0,
"aggs":{
"stats_age":{
"stats": {
"field": "age"
}
}
}
}
Extended Stats
GET test_search_index/_search
{
"size":0,
"aggs":{
"exstats_salary":{
"extended_stats": {
"field": "salary"
}
}
}
}
Percentile
GET test_search_index/_search
{
"size":0,
"aggs":{
"per_salary":{
"percentiles": {
"field": "salary"
}
}
}
}
GET test_search_index/_search
{
"size": 0,
"aggs": {
"per_age": {
"percentiles": {
"field": "salary",
"percents": [
95,
99,
99.9
]
}
}
}
}
Percentile Rank
GET test_search_index/_search
{
"size": 0,
"aggs": {
"per_salary": {
"percentile_ranks": {
"field": "salary",
"values": [
11000,
30000
]
}
}
}
}
Top Hits
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword",
"size": 10
},
"aggs": {
"top_employee": {
"top_hits": {
"size": 10,
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
}
}
}
}
}
Terms
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs": {
"terms": {
"field": "job",
"size": 5
}
}
}
}
Range
GET test_search_index/_search
{
"size": 0,
"aggs": {
"salary_range": {
"range": {
"field": "salary",
"ranges": [
{
"key":"<10000",
"to": 10000
},
{
"from": 10000,
"to": 20000
},
{
"key":">20000",
"from": 20000
}
]
}
}
}
}
Date Range
GET test_search_index/_search
{
"size": 0,
"aggs": {
"date_range": {
"range": {
"field": "birth",
"format": "yyyy",
"ranges": [
{
"from":"1980",
"to": "1990"
},
{
"from": "1990",
"to": "2000"
},
{
"from": "2000"
}
]
}
}
}
}
Histogram
GET test_search_index/_search
{
"size":0,
"aggs":{
"salary_hist":{
"histogram": {
"field": "salary",
"interval": 5000,
"extended_bounds": {
"min": 0,
"max": 40000
}
}
}
}
}
Date Histogram
GET test_search_index/_search
{
"size":0,
"aggs":{
"by_year":{
"date_histogram": {
"field": "birth",
"interval": "year",
"format":"yyyy"
}
}
}
}
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword",
"size": 10
},
"aggs": {
"age_range": {
"range": {
"field": "age",
"ranges": [
{
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30
}
]
}
}
}
}
}
}
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword",
"size": 10
},
"aggs": {
"salary": {
"stats": {
"field": "salary"
}
}
}
}
}
}
Min Bucket
GET test_search_index/_search
{
"size":0,
"aggs":{
"jobs":{
"terms": {
"field": "job.keyword",
"size": 10
},
"aggs":{
"avg_salary":{
"avg": {
"field": "salary"
}
}
}
},
"min_salary_by_job":{
"min_bucket": {
"buckets_path": "jobs>avg_salary"
}
}
}
}
Derivative
GET test_search_index/_search
{
"size": 0,
"aggs": {
"birth": {
"date_histogram": {
"field": "birth",
"interval": "year",
"min_doc_count": 0
},
"aggs": {
"avg_salary": {
"avg": {
"field": "salary"
}
},
"derivative_avg_salary": {
"derivative": {
"buckets_path": "avg_salary"
}
}
}
}
}
}
Moving Average
Cumulative Sum
filter
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs_salary_small": {
"filter": {
"range": {
"salary": {
"to": 10000
}
}
},
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword"
}
}
}
},
"jobs": {
"terms": {
"field": "job.keyword"
}
}
}
}
post_filter
GET test_search_index/_search
{
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword"
}
}
},
"post_filter": {
"match":{
"job.keyword":"java engineer"
}
}
}
global
GET test_search_index/_search
{
"query": {
"match": {
"job.keyword": "java engineer"
}
},
"aggs": {
"java_avg_salary": {
"avg": {
"field": "salary"
}
},
"all": {
"global": {},
"aggs": {
"avg_salary": {
"avg": {
"field": "salary"
}
}
}
}
}
}
GET test_search_index/_search
{
"size": 0,
"aggs": {
"jobs": {
"terms": {
"field": "job.keyword",
"size": 10,
"order": [
{
"avg_salary": "desc"
}
]
},
"aggs": {
"avg_salary": {
"avg": {
"field": "salary"
}
}
}
}
}
}
数据建模的过程
Mapping字段的相关设置
设定流程
PUT blog_index
{
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 100
}
},
"store": true
},
"publish_date": {
"type": "date",
"store": true
},
"author": {
"type": "keyword",
"ignore_above": 100,
"store": true
},
"abstract": {
"type": "text",
"store": true
},
"content": {
"type": "text",
"store": true
},
"url": {
"type": "keyword",
"doc_values":false,
"norms":false,
"ignore_above": 100,
"store": true
}
}
}
}
}
GET blog_index/_search
{
"stored_fields": ["title","publish_date","author","abstract","url"],
"query": {
"match": {
"content": "blog"
}
},
"highlight": {
"fields":{
"content": {}
}
}
}
关联关系处理
DELETE blog_index_nested
PUT blog_index_nested
{
"mappings": {
"doc":{
"properties": {
"title":{
"type": "text",
"fields": {
"keyword":{
"type":"keyword",
"ignore_above": 100
}
}
},
"publish_date":{
"type":"date"
},
"author":{
"type":"keyword",
"ignore_above": 100
},
"abstract":{
"type": "text"
},
"url":{
"enabled":false
},
"comments":{
"type":"nested",
"properties": {
"username":{
"type":"keyword",
"ignore_above":100
},
"date":{
"type":"date"
},
"content":{
"type":"text"
}
}
}
}
}
}
}
PUT blog_index_nested/doc/2
{
"title": "Blog Number One",
"author": "alfred",
"comments": [
{
"username": "lee",
"date": "2017-01-02",
"content": "awesome article!"
},
{
"username": "fax",
"date": "2017-04-02",
"content": "thanks!"
}
]
}
GET blog_index_nested/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.username": "lee"
}
},
{
"match": {
"comments.content": "thanks"
}
}
]
}
}
}
}
}
PUT blog_index_parent_child
{
"mappings": {
"doc": {
"properties": {
"join": {
"type": "join",
"relations": {
"blog": "comment"
}
}
}
}
}
}
PUT blog_index_parent_child/doc/1
{
"title":"blog",
"join":"blog" 指明父类型
}
PUT blog_index_parent_child/doc/2
{
"title":"blog2",
"join":"blog"
}
PUT blog_index_parent_child/doc/comment-1?routing=1 指明routing值,确保父子文档在一个分片上,一般使用父文档Id
{
"comment":"comment world",
"join":{
"name":"comment", 指明子类型
"parent":1 指明父文档Id
}
}
PUT blog_index_parent_child/doc/comment-2?routing=2
{
"comment":"comment hello",
"join":{
"name":"comment",
"parent":2
}
}
GET blog_index_parent_child/_search
{
"query":{
"parent_id":{
"type":"comment",
"id":"2"
}
}
}
GET blog_index_parent_child/_search
{
"query":{
"has_child": {
"type": "comment",
"query": {
"match": {
"comment": "world"
}
}
}
}
}
GET blog_index_parent_child/_search
{
"query":{
"has_parent": {
"parent_type": "blog",
"query": {
"match": {
"title": "blog"
}
}
}
}
}
nested object
parent child
建议尽量选择nested object来解决问题
POST blog_index/_update_by_query?conflicts=proceed
POST _reindex
{
"source": {
"index": "blog_index"
},
"dest": {
"index": "blog_new_index"
}
}
POST blog_index/_update_by_query?conflicts=proceed&wait_for_completion=false
GET _tasks/_qKI6E8_TDWjXyo_x-bhmw:11996
数据模型版本管理
防止字段过多
大家可以关注我的微信公众号一起学习进步。