Elasticsearch Restful API

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
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值