Elasticsearch Prefix query 前缀查询

Elasticsearch Prefix query 前缀查询

摘要

返回包含指定前缀的所有文档;简单来说,就是文档某个field字段的前几个单词的前缀为value,则满足匹配条件。

参数

field,想要搜索的字段

field 下级参数

value 必选项,前缀

请求示例

GET blak_new/_search
{
  "query": {
    "prefix": {
      "addres": {
        "value": "str"
      }
    }
  }
}

 /* 简化版 */
GET blak_new/_search
{
    "query": {
        "prefix" : { "addres" : "str" }
    }
}

返回结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 389,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "blak_new",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 6,
          "balance" : 5686,
          "firstname" : "Hattie",
          "lastname" : "Bond",
          "age" : 36,
          "gender" : "M",
          "address" : "671 Bristol Street",
          "employer" : "Netagy",
          "email" : "[email protected]",
          "city" : "Dante",
          "state" : "TN"
        }
      }
      /* 省略其他结果 */
    ]
  }
}

注意事项

  1. 加快前缀查询;可以使用 index_prefixes 参数,加快查询速度;如果使用,Elasticsearch 会在一个单独的字段中索引2到5个字符之间的前缀。这使得Elasticsearch 可以以更大的索引为代价,更有效的运行前缀索引。
  2. 允许昂贵查询,如果search.allow_expensive_queries 设置为false,则不会执行前缀查询。但是,如果index_prefixes启用,则会构建一个优化的查询,该查询并不算慢,尽管有此设置也将执行该查询。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章