Elasticsearch 结构化搜索、keyword、Term查询

1,519 阅读4分钟

前言

Elasticsearch 中的结构化搜索,即面向数值、日期、时间、布尔等类型数据的搜索,这些数据类型格式精确,通常使用基于词项的term精确匹配或者prefix前缀匹配。本文还将新版本的“text”,“keyword”进行说明,还有Term查询。

结构化搜索

结构化搜索(Structured search) 是指对结构化的数据进行搜索。比如日期、时间和数字都是结构化的,它们有精确的格式,我们可以对这些格式进行逻辑操作。比较常见的操作包括比较数字或时间的范围、判定两个值的大小、前缀匹配等。

文本也可以是结构化的。如彩色笔可以有离散的颜色集合: 红(red) 、 绿(green) 、 蓝(blue) 。一个博客可能被标记了关键词 分布式(distributed) 和 搜索(search) 。电商网站上的商品都有 UPCs(通用产品码 Universal Product Codes)或其他的唯一标识,它们都需要遵从严格规定的、结构化的格式。

在结构化查询中,我们得到的结果只有“是”或“否”两个值,可以根据场景需要,决定结构化搜索是否需要打分,但通常我们是不需要打分的。

精确值查找

让我们以下面的例子开始介绍,创建并索引一些表示产品的文档,文档里有字段 priceproductIDshowcreatedAttags价格产品ID是否展示创建时间打标信息

POST products/_doc/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3", "show":true, "createdAt":"2021-03-03", "tags":"abc" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5", "show":true, "createdAt":"2021-03-04" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7", "show":false, "createdAt":"2021-03-05"}
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8", "show":true, "createdAt":"2021-03-06"}

数字

现在我们想要做的是查找具有某个价格的所有产品,假设我们要获取价格是20元的商品,我们可以使用 term 查询,如下

GET products/_search
{
  "query": {
    "term": {
      "price": 20
    }
  }
}

通常查找一个精确值的时候,我们不希望对查询进行评分计算。只希望对文档进行包括或排除的计算,所以我们会使用 constant_score 查询以非评分模式来执行 term 查询并以1.0作为统一评分。

最终组合的结果是一个 constant_score 查询,它包含一个 term 查询:

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}

对于数字,一般还有范围查询

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

range 支持的选项

  • gt: > 大于(greater than)
  • lt: < 小于(less than)
  • gte: >= 大于或等于(greater than or equal to)
  • lte: <= 小于或等于(less than or equal to)

布尔值

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "show": true
        }
      }
    }
  }
}

日期

搜索一定时间范围内的文档

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createdAt": {
            "gte": "now-9d"
          }
        }
      }
    }
  }
}

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createdAt": {
            "gte": "2021-01-05"
          }
        }
      }
    }
  }
}

日期匹配表达式

  • y 年
  • M 月
  • w 周
  • d 天
  • H/h 小时
  • m 分钟
  • s 秒

文本

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "productID.keyword": [
            "XHDK-A-1293-#fJ3",
            "KDKE-B-9947-#kL5"
          ]
        }
      }
    }
  }
}

“productID.keyword”中的“keyword”不是关键字,而是Elasticsearch在插入文档的时候,自动为“productID”生成的子字段,名字是“keyword”。

null 处理

存在用“exists”,不存在用“must_not”搭配“exists”

// 存在“tags”字段
POST products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists": {
                    "field":"tags"
                }
            }
        }
    }
}

// 不存在“tags”字段,老版本用“missing”关键字,现在已经废除了
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "tags"
            }
          }
        }
      }
    }
  }
}

注意,新版本不要再使用“missing”关键字,现在已经废除了,用“must_not”做取反。 使用“missing”会报错,报错信息如下:

"reason": "no [query] registered for [missing]"

keyword

在2.x版本里面文本使用的是string字段。 5.0之后,把string字段设置为了过时字段,引入text与keyword字段,这两个字段都可以存储字符串使用。

“text”用于全文搜索,“keyword”用于结构化搜索。“keyword”类似Java中的枚举。在新版本中,如果没有自己创建mapping,那么在文本的处理中,会把文本自动映射为“text”,同时会生成一个子字段“keyword”,类型是“keyword”。

在存储上,“text”会被分词器进行分词,而“keyword”会被原样保留。比如“Rabit is jumping”,“text”的情况下可能被存储为“rabit”,“jump”,而“keyword”情况下就会存储为“Rabit is jumping”。

Term查询

在ES中,term查询,对输入不做分词,会将输入作为一个整体,在倒排索引中查找精确的词项,并且使用相关性算分公式为每个包含该词项的文档进行相关度算分。

比如上面的("productID": "QQPX-R-3956-#aD8"),会被分词为“qqpx”,“r”,“3956”,“ad8”。

“productID.keyword”的类型是keyword,所以即使使用match查询,最终也会变成Term查询。

// "productID.keyword": "qqpx-r-3956-#ad8" 没搜索出数据,其他都有
GET products/_search
{
  "query": {
    "match": {
      //"productID": "QQPX-R-3956-#aD8"
      //"productID": "qqpx"
      //"productID": "qqpx-r-3956-#ad8"
      //"productID.keyword": "QQPX-R-3956-#aD8"
      "productID.keyword": "qqpx-r-3956-#ad8"
    }
  }
}

// "productID": "qqpx""productID.keyword": "QQPX-R-3956-#aD8" 可以搜索出数据,其他不行
GET products/_search
{
  "query": {
    "term": {
      "productID": "QQPX-R-3956-#aD8"
      //"productID": "qqpx"
      //"productID": "qqpx-r-3956-#ad8"
      //"productID.keyword": "QQPX-R-3956-#aD8"
      //"productID.keyword": "qqpx-r-3956-#ad8"
    }
  }
}

资料