1. 程式人生 > 其它 >深入理解elasticsearch-讀書筆記

深入理解elasticsearch-讀書筆記

es基本查詢

  • 新增索引
put /new_test
{
  "mappings": {
    "properties": {
      "author": {
        "type": "text"
      },
      "characters": {
        "type": "text"
      },
      "copies": {
        "type": "long"
      },
      "otitle": {
        "type": "text"
      },
      "tags": {
        "type": "keyword"
      },
      "title": {
        "type": "text"
      },
      "year": {
        "type": "long"
      },
      "available": {
        "type": "boolean"
      },
      "review": {
        "type": "nested",
        "properties": {
          "nickname": {
            "type": "text"
          },
          "text": {
            "type": "text"
          },
          "stars": {
            "type": "integer"
          }
        }
      }
    }
  }
}
  • 批量插入資料
post /new_test/_bulk
{ "index" : {  "_id" : "1" } }
{"title":"All Quiet on the Western Front","otitle":"Im Westen nichts Neues","author":"Erich Maria Remarque","year":1929,"characters":["Paul Bäumer","Albert Kropp","Haie Westhus","Fredrich Müller","Stanislaus Katczinsky","Tjaden"],"tags":["novel"],"copies":1,"available":true,"section":3}
{"index":{"_id":"2"}}
{"title":"Catch-22","author":"Joseph Heller","year":1961,"characters":["John Yossarian","Captain Aardvark","Chaplain Tappman","Colonel Cathcart","Doctor Daneeka"],"tags":["novel"],"copies":6,"available":false,"section":1}
{"index":{"_id":"3"}}
{"title":"The Complete Sherlock Holmes","author":"Arthur Conan Doyle","year":1936,"characters":["Sherlock Holmes","Dr. Watson","G. Lestrade"],"tags":[],"copies": 0, "available":false, "section":12}
  • 查詢[1,3]區間的資料
get /new_test/_search
{
  "query":{
    "range":{
      "copies":{
        "gte":1,
        "lte":3
      }
    }
  }
}
  • 找出所有至少有一本的書籍,並對1950年後出版的書籍進行加權
get /new_test/_search
{
  "query":{
    "bool":{
      "must":[{"range":{"copies":{"gte":1}}}],
      "should":[{"range":{"year":{"gt":1950}}}]
        
    }
  }
}
  • 查找出所有tags欄位包含novel值的書籍
get /new_test/_search
{
  "query":{
     "term":{
       "tags":"novel"
     }
  }
}
  • 字首查詢
get /new_test/_search
{
  "query":{
     "prefix":{
       "title":"qu"
     }
  }
}
  • 匹配短語
get /new_test/_search
{
  "query":{
    "match_phrase":{
      "otitle":"nichts"
    }
  }
}