es的增刪改查

ES常用的一些命令

創(chuàng)建一個(gè)索引庫(kù):store
curl -XPUT http://192.168.182.129:9200/store/
#向store索引中添加一些書(shū)籍
curl -XPUT 'http://192.168.182.129:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'
#通過(guò)瀏覽器查詢
http://192.168.182.129:9200/store/books/1

#在linux中通過(guò)curl的方式查詢
curl -XGET 'http://192.168.182.129:9200/store/books/1'

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

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

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

# 或者通過(guò) _update  API的方式單獨(dú)更新你想要更新的(-XPOST局部更新)
curl -XPOST 'http://192.168.182.129:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 88.88
  }
}'

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

#刪除一個(gè)文檔
curl -XDELETE 'http://192.168.182.129:9200/store/books/1'


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

#指定多個(gè)值,查詢價(jià)格是35.99和99.99的
curl -XGET 'http://192.168.182.129: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://192.168.182.129:9200/bookstore/books/_search' -d '{
  "query" : {
    "filtered" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          }
      }
  }
}'



# bool過(guò)濾查詢,可以做組合過(guò)濾查詢
# 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: 條件必須滿足,相當(dāng)于 and
# should: 條件可以滿足也可以不滿足,相當(dāng)于 or
# must_not: 條件不需要滿足,相當(dāng)于 not

curl -XGET 'http://192.168.182.129: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://192.168.182.129: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范圍過(guò)濾
# SELECT * FROM books WHERE price >= 20 AND price < 100
# gt :  > 大于
# lt :  < 小于
# gte :  >= 大于等于
# lte :  <= 小于等于

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


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


http://192.168.182.129:9200/bookstore/books/_search
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容