大數據利器Elasticsearch之全文本查詢之intervals查詢

這是我參與8月更文挑戰的第8天,活動詳情查看:8月更文挑戰markdown

本Elasticsearch相關文章的版本爲:7.4.2app

全文本查詢使您可以搜索已分詞的文本字段例如電子郵件正文。使用在創建倒排索引期間應用於字段的相同分析器處理查詢字符串。post

intervals 查詢:容許對匹配項的順序和接近度進行細粒度控制的全文本查詢。spa

建立例子

POST /intervals_test/_doc/1
{
  "my_text": "my favorite food is cold porridge"
}

POST /intervals_test/_doc/2
{
  "my_text": "when it's cold my favorite food is porridge"
}
複製代碼

當各個匹配項有順序要求時:
請求體:code

GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_cat/indices


POST /intervals_test/_doc/1
{
  "my_text": "my favorite food is cold porridge"
}

POST /intervals_test/_doc/2
{
  "my_text": "when it's cold my favorite food is porridge"
}


POST /intervals_test/_search
{
  "query": {
    "intervals" : {
      "my_text" : {
        "all_of" : {
          "ordered" : true,
          "intervals" : [
            {
              "match" : {
                "query" : "my favorite food",
                "max_gaps" : 0,
                "ordered" : true
              }
            },
            {
              "any_of" : {
                "intervals" : [
                  { "match" : { "query" : "hot water" } },
                  { "match" : { "query" : "cold porridge" } }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
複製代碼

響應體:orm

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.3333333,
    "hits" : [
      {
        "_index" : "intervals_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.3333333,
        "_source" : {
          "my_text" : "my favorite food is cold porridge"
        }
      }
    ]
  }
}

複製代碼

當各個匹配項沒有順序要求時:
請求體:索引

POST /intervals_test/_search
{
  "query": {
    "intervals" : {
      "my_text" : {
        "all_of" : {
          "ordered" : false,
          "intervals" : [
            {
              "match" : {
                "query" : "my favorite food",
                "max_gaps" : 0,
                "ordered" : true
              }
            },
            {
              "any_of" : {
                "intervals" : [
                  { "match" : { "query" : "hot water" } },
                  { "match" : { "query" : "cold porridge" } }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
複製代碼

響應體:ip

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.3333333,
    "hits" : [
      {
        "_index" : "intervals_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.3333333,
        "_source" : {
          "my_text" : "my favorite food is cold porridge"
        }
      },
      {
        "_index" : "intervals_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.3333333,
        "_source" : {
          "my_text" : "when it's cold my favorite food is porridge"
        }
      }
    ]
  }
}
複製代碼

intervals查詢的類型有:文檔

  • match
  • prefix
  • wildcard
  • all_of
  • any_of
  • filter

match類型的intervals查詢

返回知足match子句條件的文檔,能夠設定查詢的字符串是否須要按照順序以及分詞之間的間隔等條件。
match類型的參數:字符串

參數 描述
query 用戶查詢的字符串
max_gaps 字符串中每一個詞在text field中出現的最大詞間距,超過最大間距的將不會被檢索到;默認值是-1,即不限制,設置爲0的話,query中的字符串必須彼此相連不能拆分
ordered query中的字符串是否須要有序顯示,默認值是false,即不考慮前後順序
analyzer 對query參數中的字符串使用什麼分詞器,默認使用mapping時該field配置的 search analyzer
filter 能夠爲query搭配一個intervals filter,該filter不一樣於Boolean filter 有本身的語法結構

其餘類型的參數和match類型的參數用法同樣。

相關文章
相關標籤/搜索