1PUT /goods
2{
3 "settings": {
4 "number_of_shards": 1,
5 "number_of_replicas": 0
6 }
7}
11DELETE /goods
11GET /goods
161PUT /goods/_mapping/docs
2{
3 "properties": {
4 "title":{
5 "type": "text",
6 "analyzer": "ik_max_word"
7 },
8 "images":{
9 "type": "keyword",
10 "index": false
11 },
12 "price":{
13 "type": "double"
14 }
15 }
16}
11GET /goods/_mapping
x1POST /goods/docs/1
2{
3 "title":"小米手机",
4 "images":"http://image.leyou.com/12479122.jpg",
5 "price":2899.00
6}
7
8tips:新增数据中存在映射没有的字段,则会自动更新映射信息
81PUT /goods/docs/1
2{
3 "title":"小米手机",
4 "images":"http://image.leyou.com/12479122.jpg",
5 "price":2799.00
6}
7
8tips:只需将新增的POST请求改为PUT就行了
11DELETE /goods/docs/1
371PUT /cars
2{
3 "settings": {
4 "number_of_shards": 1,
5 "number_of_replicas": 0
6 },
7 "mappings": {
8 "transactions": {
9 "properties": {
10 "color": {
11 "type": "keyword"
12 },
13 "make": {
14 "type": "keyword"
15 }
16 }
17 }
18 }
19}
20
21POST /cars/transactions/_bulk
22{ "index": {}}
23{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
24{ "index": {}}
25{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
26{ "index": {}}
27{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
28{ "index": {}}
29{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
30{ "index": {}}
31{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
32{ "index": {}}
33{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
34{ "index": {}}
35{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
36{ "index": {}}
37{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
61GET /goods/_search
2{
3 "query": {
4 "match_all": {}
5 }
6}
81GET /goods/_search
2{
3 "query": {
4 "match": {
5 "title": "小米手机"
6 }
7 }
8}
111GET /goods/_search
2{
3 "query": {
4 "match": {
5 "title": {
6 "query": "小米手机",
7 "operator": "and"
8 }
9 }
10 }
11}
111GET /goods/_search
2{
3 "query": {
4 "match": {
5 "title": {
6 "query": "小米手机",
7 "minimum_should_match": "75%"
8 }
9 }
10 }
11}
91GET /goods/_search
2{
3 "query": {
4 "multi_match": {
5 "query": "小米",
6 "fields": ["title","subTitle"]
7 }
8 }
9}
171GET goods/_search
2{
3 "query": {
4 "term": {
5 "price": 2799.00
6 }
7 }
8}
9
10GET /goods/_search
11{
12 "query":{
13 "terms":{
14 "price":[2799.00,2899.00,3899.00]
15 }
16 }
17}
111GET /goods/_search
2{
3 "query": {
4 "range": {
5 "price": {
6 "gte": 2500,
7 "lte": 3000
8 }
9 }
10 }
11}
111GET goods/_search
2{
3 "query": {
4 "fuzzy": {
5 "title": {
6 "value": "小木",
7 "fuzziness": 2
8 }
9 }
10 }
11}
311GET goods/_search
2{
3 "query": {
4 "bool": {
5 "must": [
6 {"match": {
7 "title": "手机"
8 }}
9 ],
10 "must_not": [
11 {"term": {
12 "price": {
13 "value": "2799"
14 }
15 }}
16 ],
17 "should": [
18 {"multi_match": {
19 "query": "小米",
20 "fields": ["title","subTitle"]
21 }
22 }
23 ]
24 }
25 }
26}
27
28上述语句地含义为:
291. 必须满足:"title包含手机"
302. 不能满足:"price为2799"
313. 最好满足:"title或subTitle包含小米"
201GET goods/_search
2{
3 "query": {
4 "bool": {
5 "must": [
6 {"match": {
7 "title": "小米"
8 }}
9 ],
10 "filter": {
11 "range": {
12 "price": {
13 "gte": 2699,
14 "lte": 2799
15 }
16 }
17 }
18 }
19 }
20}
171GET /goods/_search
2{
3 "_source": ["title","price"],
4 "query": {
5 "match_all": {}
6 }
7}
8
9GET /goods/_search
10{
11 "_source": {
12 "excludes": ["price","images"]
13 },
14 "query": {
15 "match_all": {}
16 }
17}
131GET goods/_search
2{
3 "query": {
4 "match_all": {}
5 },
6 "sort": [
7 {
8 "price": {
9 "order": "desc"
10 }
11 }
12 ]
13}
101GET /goods/_search
2{
3 "query": {
4 "match_all": {}
5 },
6 "sort": [
7 { "price": { "order": "desc" }},
8 { "_score": { "order": "desc" }}
9 ]
10}
111GET /cars/_search
2{
3 "size": 0,
4 "aggs": {
5 "popular_colors": {
6 "terms": {
7 "field": "color"
8 }
9 }
10 }
11}
181GET /cars/_search
2{
3 "size": 0,
4 "aggs": {
5 "popular_colors": {
6 "terms": {
7 "field": "color"
8 },
9 "aggs": {
10 "avg_price": {
11 "avg": {
12 "field": "price"
13 }
14 }
15 }
16 }
17 }
18}
231GET /cars/_search
2{
3 "size": 0,
4 "aggs": {
5 "popular_colors": {
6 "terms": {
7 "field": "color"
8 },
9 "aggs": {
10 "avg_price": {
11 "avg": {
12 "field": "price"
13 }
14 },
15 "maker":{
16 "terms": {
17 "field": "make"
18 }
19 }
20 }
21 }
22 }
23}
131GET cars/_search
2{
3 "size": 0,
4 "aggs": {
5 "price_agg": {
6 "histogram": {
7 "field": "price",
8 "interval": 5000,
9 "min_doc_count": 1
10 }
11 }
12 }
13}