1. 程式人生 > 實用技巧 >ES操作詳解

ES操作詳解

1、es是基於lucene開發的一個分散式全文索引框架。往ES中儲存和從ES中查詢,格式為Json
索引:Index 相當於資料庫中的database
型別:type 相當於資料庫中的table
主鍵:id 相當於資料庫中的主鍵

往ES中儲存資料,其實就是往ES中的index下的type儲存Json資料

RESTFul風格API
通過http的形式,傳送請求,對ES進行操作
查詢: GET
刪除: DELETE
新增: PUT/POST
修改: PUT/POST

1、PUT操作,store為database;books為table;2為主鍵

curl -XPUT 'http://hadoop100:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

2、GET操作

a)根據id查詢

curl -XGET 'http://hadoop100:9200/store/books/1'  

b)通過_source獲取指定的欄位

curl -XGET 'http://hadoop100:9200/store/books/1?_source=title
' curl -XGET 'http://hadoop100:9200/store/books/1?_source=title,price' curl -XGET 'http://hadoop100:9200/store/books/1?_source'

c)最簡單_search 結合filter查詢

curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "price": 35.99
} } } } }'
#指定多個值
curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
    "query" : {
        "bool" : {
            "filter" : {
                "terms" : {
                    "price" : [35.99, 99.99]
                  }
              }
        }
    }
}'
curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
  "query" : {
    "bool" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          }
      }
  }
}'

d)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" : [],
#    }
#  }
#
# must: 條件必須滿足,相當於 and
# should: 條件可以滿足也可以不滿足,相當於 or
# must_not: 條件不需要滿足,相當於 not

curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
  "query" : {
    "bool" : {
      "should" : [
        { "term" : {"price" : 35.99}},
        { "term" : {"price" : 99.99}}
      ],
      "must_not" : {
        "term" : {"publish_date" : "2016-02-06"}
      }
    }
  }
}'

e)巢狀查詢

# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 )

curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "price": 35.99
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "publish_date": "2016-02-06"
                                }
                            },
                            {
                                "term": {
                                    "price": 99.99
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}'

f)range範圍過濾

# SELECT * FROM books WHERE price >= 10 AND price < 99
# gt :  > 大於
# lt :  < 小於
# gte :  >= 大於等於
# lte :  <= 小於等於

curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
    "query": {
        "range" : {
            "price" : {
                "gte" : 10,
                "lt" : 99
            }
        }
    }
}
#name和author都必須包含Guide,並且價錢等於33.99或者188.99
curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "operator": "and",
                    "fields": [
                        "name",
                        "author"
                    ],
                    "query": "Guide"
                }
            },
            "filter": {
                "terms": {
                    "price": [
                        35.99,
                        188.99
                    ]
                }
            }
        }
    }
}'

3、UPDATE操作

a)以通過覆蓋的方式更新

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"
}'

b)通過 _update API的方式單獨更新你想要更新的

curl -XPOST 'http://192.168.10.16:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 88.88
  }
}'

4、DELETE操作

curl -XDELETE 'http://192.168.10.16:9200/store/books/1'

5、查詢所有(_search)

http://hadoop100:9200/store/books/_search