天天看點

Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

RESTful接口URL的格式:

http://IP:9200/<index>/<type>/[<id>]      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • 其中index、type是必須提供的。
  • id是可選的,不提供es會自動生成。
  • index、type将資訊進行分層,利于管理。
  • index可以了解為資料庫;type了解為資料表;id相當于資料庫表中記錄的主鍵,是唯一的。

RESTful接口URL的增删改查:

添加資料

  • #向store索引中添加一些書籍
curl -XPUT 'http://IP:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • #再添加一個書的資訊
curl -XPUT 'http://IP:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

查詢

  • #在linux中通過curl的方式查詢
curl -XGET 'http://IP:9200/store/books/1'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • #通過浏覽器查詢
http://IP:9200/store/books/1      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # 通過_source擷取指定的字段
curl -XGET 'http://IP:9200/store/books/1?_source=title'
curl -XGET 'http://IP:9200/store/books/1?_source=title,price'
curl -XGET 'http://IP:9200/store/books/1?_source'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

更新

  • #可以通過覆寫的方式更新
curl -XPUT 'http://IP:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2016-02-06",
  "price":"99.99"
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # 或者通過 _update  API的方式單獨更新你想要更新的
curl -XPOST 'http://IP:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 88.88
  }
}'
curl -XGET 'http://IP:9200/store/books/1'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

删除

  • #删除一個文檔
curl -XDELETE 'http://IP:9200/store/books/1'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

進階查詢

最簡單filter查詢

  • 類似 :SELECT * FROM books WHERE price = 35.99
# 首先添加一條資料
curl -XPUT 'http://IP:9200/store/books/4' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "author": "Guide",
  "publish_date":"2016-02-06",
  "price":"35.99"
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # filtered 查詢價格是35.99的
  • # 傳回的的分是1.0
curl -XGET 'http://IP:9200/store/books/_search' -d '{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "price": 35.99
        }
      }
    }
  }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
curl -XGET 'http://IP:9200/store/books/_search' -d '{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": 35.99
        }
      }
    }
  }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # 傳回的的分是0.0

指定多個值查詢

curl -XGET 'http://IP:9200/store/books/_search' -d '{
    "query" : {
        "bool" : {
            "filter" : {
                "terms" : {
                    "price" : [35.99, 99.99]
                  }
              }
        }
    }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
curl -XGET 'http://IP:9200/store/books/_search' -d '{
    "query" : {
        "bool" : {
            "must": {
                "match_all": {}
            },
            "filter" : {
                "terms" : {
                    "price" : [35.99, 99.99]
                  }
              }
        }
    }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # SELECT * FROM books WHERE publish_date = "2015-02-06"
curl -XGET 'http://IP:9200/store/books/_search' -d '{
  "query" : {
    "bool" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          }
      }
  }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

嵌套查詢

# bool過濾查詢,可以做組合過濾查詢

  • 類似: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" : [],
    }
  }      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • # must: 條件必須滿足,相當于 and
  • # should: 條件可以滿足也可以不滿足,相當于 or
  • # must_not: 條件不需要滿足,相當于 not
curl -XGET 'http://IP:9200/store/books/_search' -d '{
  "query" : {
    "bool" : {
      "should" : [
        { "term" : {"price" : 35.99}},
        { "term" : {"price" : 99.99}}
      ],
      "must_not" : {
        "term" : {"publish_date" : "2016-02-06"}
      }
    }
  }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

 range範圍過濾

  • 類似: SELECT * FROM books WHERE price >= 10 AND price < 99
    • # gt :  > 大于
    • # lt :  < 小于
    • # gte :  >= 大于等于
    • # lte :  <= 小于等于
curl -XGET 'http://IP:9200/store/books/_search' -d '{
    "query": {
        "range" : {
            "price" : {
                "gte" : 10,
                "lt" : 99
            }
        }
    }
}      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:
  • #name和author都必須包含Guide,并且價錢等于33.99或者188.99
curl -XGET 'http://IP:9200/store/books/_search' -d '{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "operator": "and",
                    "fields": [
                        "name",
                        "author"
                    ],
                    "query": "Guide"
                }
            },
            "filter": {
                "terms": {
                    "price": [
                        35.99,
                        188.99
                    ]
                }
            }
        }
    }
}'      
Elasticsearch:RESTful風格APIRESTful接口URL的格式:RESTful接口URL的增删改查:

參考來源:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html