http://192.168.10.16:9200/<index>/<type>/[<id>]
index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。
增
curl -XPUT 'http://192.168.1.161:9200/store/books/1' -d '{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2015-02-06",
"price":"49.99"
}'
删
curl -XDELETE 'http://192.168.10.16:9200/store/books/1'
改
完全覆盖
curl -XPUT 'http://192.168.10.16:9200/store/books/1' -d '{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2016-02-06",
"price":"99.99"
}'
指定字段改变
curl -XPOST 'http://192.168.10.16:9200/store/books/1/_update' -d '{
"doc": {
"price" : 88.88
}
}'
查
查询整个key
curl -XGET 'http://192.168.10.18:9200/store/books/1'
指定需要查询的字段
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source=title,price'
条件查询最简单filter查询
SELECT * FROM books WHERE price = 35.99
filtered 查询价格是35.99的
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"price": 35.99
}
}
}
}
}'
多个值
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"price" : [35.99, 99.99]
}
}
}
}
}'
组合过滤条件
SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND publish_date != “2016-02-06”
类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式
格式如下:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
must: 条件必须满足,相当于 and
should: 条件可以满足也可以不满足,相当于 or
must_not: 条件不需要满足,相当于 not
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"should" : [
{ "term" : {"price" : 35.99}},
{ "term" : {"price" : 99.99}}
],
"must_not" : {
"term" : {"publish_date" : "2016-02-06"}
}
}
}
}'
range范围过滤
SELECT * FROM books WHERE price >= 10 AND price < 99
gt : > 大于
lt : < 小于
gte : >= 大于等于
lte : <= 小于等于
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"range" : {
"price" : {
"gte" : 10,
"lt" : 99
}
}
}
}