Elasticsearch 查詢語法


基本查詢  

GET /索引庫名/_search
{
    "query":{
        "查詢類型":{
            "查詢條件":"查詢條件值"
        }
    }
}

 

這里的query代表一個查詢對象,里面可以有不同的查詢屬性

  • 查詢類型:

    • 例如:match_all, matchterm , range 等等

  • 查詢條件:查詢條件會根據類型的不同,寫法也有差異

 

查詢所有(match_all)

GET /test/_search
{
    "query":{
        "match_all": {}
    }
}
  • query:代表查詢對象

  • match_all:代表查詢所有

結果 

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "大米手機",
          "images": "http://image.test.com/12479122.jpg",
          "price": 2899
        }
      },
      {
        "_index": "test",
        "_type": "goods",
        "_id": "r9c1KGMBIhaxtY5rlRKv",
        "_score": 1,
        "_source": {
          "title": "小米手機",
          "images": "http://image.test.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}
  • took:查詢花費時間,單位是毫秒

  • time_out:是否超時

  • _shards:分片信息

  • hits:搜索結果總覽對象

    • total:搜索到的總條數

    • max_score:所有結果中文檔得分的最高分

    • hits:搜索結果的文檔對象數組,每個元素是一條搜索到的文檔信息

      • _index:索引庫

      • _type:文檔類型

      • _id:文檔id

      • _score:文檔得分

      • _source:文檔的源數據

 

匹配查詢(match)

match類型查詢,會把查詢條件進行分詞,然后進行查詢,多個詞條之間是or的關系

GET /test/_search
{
    "query":{
        "match":{
            "title":"小米電視"
        }
    }
}

結果  

"hits": {
    "total": 2,
    "max_score": 0.6931472,
    "hits": [
        {
            "_index": "test",
            "_type": "goods",
            "_id": "tmUBomQB_mwm6wH_EC1-",
            "_score": 0.6931472,
            "_source": {
                "title": "小米手機",
                "images": "http://image.test.com/12479122.jpg",
                "price": 2699
            }
        },
        {
            "_index": "test",
            "_type": "goods",
            "_id": "3",
            "_score": 0.5753642,
            "_source": {
                "title": "小米電視4A",
                "images": "http://image.test.com/12479122.jpg",
                "price": 3899
            }
        }
    ]
}
  • and關系

某些情況下,我們需要更精確查找,我們希望這個關系變成and,可以這樣做:

GET /test/_search
{
    "query":{
        "match": {
          "title": {
            "query": "小米電視",
            "operator": "and"
          }
        }
    }
}

orand 間二選一有點過於非黑即白。 如果用戶給定的條件分詞后有 5 個查詢詞項,想查找只包含其中 4 個詞的文檔,該如何處理?將 operator 操作符參數設置成 and 只會將此文檔排除。

有時候這正是我們期望的,但在全文搜索的大多數應用場景下,我們既想包含那些可能相關的文檔,同時又排除那些不太相關的。換句話說,我們想要處於中間某種結果。

match 查詢支持 minimum_should_match 最小匹配參數, 這讓我們可以指定必須匹配的詞項數用來表示一個文檔是否相關。我們可以將其設置為某個具體數字,更常用的做法是將其設置為一個百分數,因為我們無法控制用戶搜索時輸入的單詞數量:

 

GET /test/_search
{
    "query":{
        "match":{
            "title":{
                "query":"小米曲面電視",
                "minimum_should_match": "75%"
            }
        }
    }
}

本例中,搜索語句可以分為3個詞,如果使用and關系,需要同時滿足3個詞才會被搜索到。這里我們采用最小品牌數:75%,那么也就是說只要匹配到總詞條數量的75%即可,這里3*75% 約等於2。所以只要包含2個詞條就算滿足條件了。

 

多字段查詢(multi_match)

multi_matchmatch類似,不同的是它可以在多個字段中查詢

GET /test/_search
{
    "query":{
        "multi_match": {
            "query":    "小米",
            "fields":   [ "title", "subTitle" ]
        }
    }
}

詞條匹配(term)

term 查詢被用於精確值 匹配,這些精確值可能是數字、時間、布爾或者那些未分詞的字符串

 

GET /test/_search
{
    "query":{
        "term":{
            "price":2699.00
        }
    }
}

多詞條精確匹配(terms)

terms 查詢和 term 查詢一樣,但它允許你指定多值進行匹配。如果這個字段包含了指定值中的任何一個值,那么這個文檔滿足條件:

GET /test/_search
{
    "query":{
        "terms":{
            "price":[2699.00,2899.00,3899.00]
        }
    }
}

 

結果過濾

默認情況下,elasticsearch在搜索的結果中,會把文檔中保存在_source的所有字段都返回。

如果我們只想獲取其中的部分字段,我們可以添加_source的過濾

 

直接指定字段

GET /test/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 2699
    }
  }
}

 

指定includes和excludes

  • includes:來指定想要顯示的字段

  • excludes:來指定不想要顯示的字段

GET /test/_search
{
  "_source": {
    "includes":["title","price"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}
GET /test/_search
{
  "_source": {
     "excludes": ["images"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}

高級查詢

布爾組合(bool)

  bool把各種其它查詢通過must(與)、must_not(非)、should(或)的方式進行組合

 

GET /test/_search
{
    "query":{
        "bool":{
            "must":     { "match": { "title": "大米" }},
            "must_not": { "match": { "title":  "電視" }},
            "should":   { "match": { "title": "手機" }}
        }
    }
}
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "title": "大米手機",
          "images": "http://image.test.com/12479122.jpg",
          "price": 2899
        }
      }
    ]
  }
}

范圍查詢(range)

range 查詢找出那些落在指定區間內的數字或者時間

GET /test/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  1000.0,
                "lt":   2800.00
            }
        }
    }
}
`range`查詢允許以下字符:

| 操作符 |   說明   |
| :----: | :------: |
|   gt   |   大於   |
|  gte   | 大於等於 |
|   lt   |   小於   |
|  lte   | 小於等於 |

模糊查詢(fuzzy)

fuzzy 查詢是 term 查詢的模糊等價。它允許用戶搜索詞條與實際詞條的拼寫出現偏差,但是偏差的編輯距離不得超過2:

GET /test/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

上面的查詢,也能查詢到apple手機

我們可以通過fuzziness來指定允許的編輯距離:

GET /test/_search
{
  "query": {
    "fuzzy": {
        "title": {
            "value":"appla",
            "fuzziness":1
        }
    }
  }
}

過濾(filter)

 

條件查詢中進行過濾

所有的查詢都會影響到文檔的評分及排名。如果我們需要在查詢結果中進行過濾,並且不希望過濾條件影響評分,那么就不要把過濾條件作為查詢條件來用。而是使用filter方式:

GET /test/_search
{
    "query":{
        "bool":{
            "must":{ "match": { "title": "小米手機" }},
            "filter":{
                "range":{"price":{"gt":2000.00,"lt":3800.00}}
            }
        }
    }
}

filter中還可以再次進行bool組合條件過濾。

無查詢條件,直接過濾

如果一次查詢只有過濾,沒有查詢條件,不希望進行評分,我們可以使用constant_score取代只有 filter 語句的 bool 查詢。在性能上是完全相同的,但對於提高查詢簡潔性和清晰度有很大幫助。

GET /test/_search
{
    "query":{
        "constant_score":   {
            "filter": {
                 "range":{"price":{"gt":2000.00,"lt":3000.00}}
            }
        }
}

排序

單字段排序

sort 可以讓我們按照不同的字段進行排序,並且通過order指定排序的方式

GET /test/_search
{
  "query": {
    "match": {
      "title": "小米手機"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

多字段排序

假定我們想要結合使用 price和 _score(得分) 進行查詢,並且匹配的結果首先按照價格排序,然后按照相關性得分排序:

GET /goods/_search
{
    "query":{
        "bool":{
            "must":{ "match": { "title": "小米手機" }},
            "filter":{
                "range":{"price":{"gt":200000,"lt":300000}}
            }
        }
    },
    "sort": [
      { "price": { "order": "desc" }},
      { "_score": { "order": "desc" }}
    ]
}

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM