探索ES查詢(xún)語(yǔ)法

以下是 Elasticsearch 主要查詢(xún)類(lèi)型的示例及說(shuō)明:


一、全文檢索查詢(xún)

  1. Match Query
    搜索分詞后的文本字段(如 text 類(lèi)型):

    GET /products/_search
    {
      "query": {
        "match": {
          "description": "wireless headphones"
        }
      }
    }
    
    • 自動(dòng)對(duì) "wireless headphones" 分詞(如拆為 wirelessheadphones),匹配任一詞匯的文檔。
  2. Multi-match Query
    跨多個(gè)字段搜索:

    {
      "query": {
        "multi_match": {
          "query": "apple",
          "fields": ["title", "brand", "description"]
        }
      }
    }
    

二、精確查詢(xún)

  1. Term Query
    精確匹配未分詞的 keyword 字段:

    {
      "query": {
        "term": {
          "status": {
            "value": "published"  // 精確匹配字段值
          }
        }
      }
    }
    
  2. Terms Query
    匹配字段中包含任一指定值的文檔:

    {
      "query": {
        "terms": {
          "tags": ["electronics", "sale"]  // 匹配含 electronics 或 sale 的文檔
        }
      }
    }
    
  3. Range Query
    數(shù)值或日期范圍過(guò)濾:

    {
      "query": {
        "range": {
          "price": {
            "gte": 100,
            "lte": 1000
          }
        }
      }
    }
    
  4. Exists Query
    篩選存在某字段的文檔:

    {
      "query": {
        "exists": {
          "field": "author"  // 返回包含 author 字段的文檔
        }
      }
    }
    

三、復(fù)合查詢(xún)

  1. Bool Query 組合邏輯條件(AND/OR/NOT):

    {
      "query": {
        "bool": {
          "must": [  // 必須滿(mǎn)足
            { "match": { "title": "phone" } },
            { "range": { "price": { "lte": 500 } } }
          ],
          "must_not": [  // 必須不滿(mǎn)足
            { "term": { "brand": "A" } }
          ],
          "should": [  // 滿(mǎn)足任意一個(gè)
            { "term": { "color": "black" } },
            { "term": { "color": "silver" } }
          ],
          "minimum_should_match": 1,  // 至少滿(mǎn)足 1 個(gè) should 條件
          "filter": [  // 過(guò)濾,不參與評(píng)分
            { "term": { "in_stock": true } }
          ]
        }
      }
    }
    

四、特殊查詢(xún)

  1. Match_all Query
    匹配所有文檔:

    {
      "query": {
        "match_all": {}
      }
    }
    
  2. Wildcard Query
    通配符匹配(* 匹配任意字符,? 匹配單個(gè)字符):

    {
      "query": {
        "wildcard": {
          "sku": "pro-*"  // 匹配 pro-123, pro-abc 等
        }
      }
    }
    
  3. Prefix Query
    前綴匹配:

    {
      "query": {
        "prefix": {
          "city": "new"  // 匹配 new york, new delhi 等
        }
      }
    }
    
  4. Fuzzy Query
    容錯(cuò)匹配(允許拼寫(xiě)錯(cuò)誤):

    {
      "query": {
        "fuzzy": {
          "text": {
            "value": "quick",  
            "fuzziness": "AUTO"  // 自動(dòng)允許 1-2 個(gè)字符的差異
          }
        }
      }
    }
    

五、其他功能示例

  1. 分頁(yè)與排序

    {
      "query": { "match_all": {} },
      "from": 10,  // 跳過(guò)前 10 條
      "size": 5,   // 返回 5 條結(jié)果
      "sort": [
        { "price": { "order": "desc" } }  // 按價(jià)格降序排序
      ]
    }
    
  2. 高亮匹配內(nèi)容

    {
      "query": {
        "match": { "content": "Elasticsearch" }
      },
      "highlight": {
        "fields": {
          "content": {}  // 高亮 content 字段中的匹配詞
        }
      }
    }
    

關(guān)鍵注意事項(xiàng)

  • 默認(rèn)返回 10 條數(shù)據(jù):在未顯式設(shè)置 size 參數(shù)時(shí),Elasticsearch 的 search 查詢(xún)默認(rèn)僅返回匹配結(jié)果的前 10 條記錄。
  • 字段類(lèi)型term 適用于 keyword 類(lèi)型,match 適用于 text 類(lèi)型。
  • 性能:優(yōu)先用 filter 替代 must 進(jìn)行精確過(guò)濾(如狀態(tài)、時(shí)間范圍),減少評(píng)分計(jì)算。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容