Elasticsearch使用filter進行匹配關係and,or,not,range查詢

RESTful接口URL的格式:
http://localhost:9200/<index>/<type>/[<id>]
其中index、type是必須提供的。
id是可選的,不提供es會自動生成。
index、type將信息進行分層,利於管理。
index可以理解爲數據庫;type理解爲數據表;id相當於數據庫表中記錄的主鍵,是唯一的。


#向store索引中添加一些書籍
curl -XPUT 'http://172.16.0.14:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'

#通過瀏覽器查詢
http://172.16.0.14:9200/store/books/1

#在linux中通過curl的方式查詢
curl -XGET 'http://172.16.0.14:9200/store/books/1'

#在添加一個書的信息
curl -XPUT 'http://172.16.0.14:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'


# 通過ID獲得文檔信息
curl -XGET 'http://172.16.0.14:9200/bookstore/books/1'

#在瀏覽器中查看
http://172.16.0.14:9200/bookstore/books/1

# 通過_source獲取指定的字段
curl -XGET 'http://172.16.0.14:9200/store/books/1?_source=title'
curl -XGET 'http://172.16.0.14:9200/store/books/1?_source=title,price'
curl -XGET 'http://172.16.0.14:9200/store/books/1?_source'

#可以通過覆蓋的方式更新
curl -XPUT 'http://172.16.0.14:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2016-02-06",
  "price":"99.99"
}'

# 或者通過 _update  API的方式單獨更新你想要更新的
curl -XPOST 'http://172.16.0.14:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 88.88
  }
}'

curl -XGET 'http://172.16.0.14:9200/store/books/1'
 
#刪除一個文檔
curl -XDELETE 'http://172.16.0.14:9200/store/books/1'


# 最簡單filter查詢
# SELECT * FROM books WHERE price = 35.99
# filtered 查詢價格是35.99的
curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{
    "query" : {
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                "term" : {
                    "price" : 35.99
                  }
              }
        }
    }
}'

#指定多個值
curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{
    "query" : {
        "filtered" : {
            "filter" : {
                "terms" : {
                    "price" : [35.99, 99.99]
                  }
              }
        }
    }
}'


# SELECT * FROM books WHERE publish_date = "2015-02-06"
curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{
  "query" : {
    "filtered" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          } 
      }
  }
}'



# 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://172.16.0.14:9200/bookstore/books/_search' -d '{
  "query" : {
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" : [
            { "term" : {"price" : 35.99}},
            { "term" : {"price" : 99.99}}
          ],
          "must_not" : {
            "term" : {"publish_date" : "2016-02-06"}
          }
        }
      }
    }
  }
}'


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

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

# range範圍過濾
# SELECT * FROM books WHERE price >= 20 AND price < 100
# gt :  > 大於
# lt :  < 小於
# gte :  >= 大於等於
# lte :  <= 小於等於

curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{
  "query" : {
    "filtered" : {
      "filter" : {
        "range" : {
          "price" : {
            "gt" : 20.0,
            "lt" : 100
          }
        }
      }
    }
  }
}'


# 另外一種 and, or, not查詢
# 沒有bool, 直接使用and , or , not
# 注意: 不帶bool的這種查詢不能利用緩存
# 查詢價格既是35.99,publish_date又爲"2015-02-06"的結果
curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{
  "query": {
    "filtered": {
      "filter": {
        "and": [
        {
          "term": {
            "price":59.99
          }
        },
        {
          "term": {
            "publish_date":"2015-02-06"
          }
        }
       ]
     },
     "query": {
      "match_all": {}
      }
    }
  }
}'

轉載來自:https://www.cnblogs.com/DreamDrive/p/6819449.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章