带你读《Elastic Stack 实战手册》之34:——3.4.2.17.3.全文搜索/精确搜索(4)

简介: 带你读《Elastic Stack 实战手册》之34:——3.4.2.17.3.全文搜索/精确搜索(4)


《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.3.全文搜索/精确搜索(3) https://developer.aliyun.com/article/1229941



三、基于词项的查询方法

 

Term 类查询主要有 term/terms/terms_set/wildcard/range/fuzzy/prefix/regexp/ids/exists 十个主要查询方法。

 

3.1 term

 

日常用法如下:


PUT term-query
POST term-query/_mapping
{"properties":{"user.id":{"type":"text"}}}
POST term-query/_bulk
{ "index": { "_id": 1 }}
{ "user.id": "kimchy" }
{ "index": { "_id": 2 }}
{ "user.id": "elkbee" }
GET term-query/_search
{
  "query": {
    "term": {
      "user.id": { 
        "value": "kimchy", 
        "boost": 1.0
      }
    }
  }
}

 主要参数:

 

boost:用于减少或增加查询的相关分数的浮点数值。默认为1.0。可以使用 boost 参数来调整包含两个或多个查询的搜索的相关性分数。Boost 值相对于默认值1.0。0到1.0之间的升压值会降低相关性得分。大于1.0的值会增加相关分数.

 

利用缓存查询

 

使用建议:可以通过 Constant Score 将查询转换成⼀个 Filter,避免算分,并利⽤缓存,提⾼性能。


# 原查询
GET my-index-000001/_search
{
  "explain": true,  
  "query": {
"term": {
      "full_text": "foxes"
    }
  }
}
# 相关返回
{
  ......
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_shard" : "[my-index-000001][0]",
        "_node" : "egEhfXrCTrycdbAwEy9z3Q",
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "full_text" : "quick brown foxes!"
        },
        "_explanation" : {
          "value" : 0.18232156,
          "description" : "weight(full_text:foxes in 0) [PerFieldSimilarity], result of:",
          "details" : [
            {
              "value" : 0.18232156,
              "description" : "score(freq=1.0), computed as boost * idf * tf from:",
              "details" : [
                {
                  "value" : 2.2,
                  "description" : "boost",
                  "details" : [ ]
                },
                {
                  "value" : 0.18232156,
                  "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                  "details" : [
                    {
                      "value" : 2,
                      "description" : "n, number of documents containing term",
                      "details" : [ ]
                    },
                    {
                      "value" : 2,
                      "description" : "N, total number of documents with field",
                      "details" : [ ]
                    }
                  ]
                },
                {
                  "value" : 0.45454544,
                  "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                  "details" : [
                    {
                      "value" : 1.0,
                      "description" : "freq, occurrences of term within document",
                      "details" : [ ]
                    },
                    {
                      "value" : 1.2,
                      "description" : "k1, term saturation parameter",
                      "details" : [ ]
                    },
                    {
                      "value" : 0.75,
                      "description" : "b, length normalization parameter",
                      "details" : [ ]
                    },
                    {
                      "value" : 3.0,
                      "description" : "dl, length of field",
                      "details" : [ ]
                    },
                    {
                      "value" : 3.0,
                      "description" : "avgdl, average length of field",
                      "details" : [ ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        "_shard" : "[my-index-000001][0]",
        "_node" : "egEhfXrCTrycdbAwEy9z3Q",
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "full_text" : "Quick Foxes Brown !"
        },
        "_explanation" : {
                  ......
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
# 利用 constant score filter 
POST my-index-000001/_search
{
  "explain": true,
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "full_text": "foxes"
        }
      }
    }
  }
}
# 相关返回
{
  ......
  "hits" : {
"total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_shard" : "[my-index-000001][0]",
        "_node" : "egEhfXrCTrycdbAwEy9z3Q",
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "full_text" : "quick brown foxes!"
        },
        "_explanation" : {
          "value" : 1.0,
          "description" : "ConstantScore(full_text:foxes)",
          "details" : [ ]
        }
      },
      {
        "_shard" : "[my-index-000001][0]",
        "_node" : "egEhfXrCTrycdbAwEy9z3Q",
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "full_text" : "Quick Foxes Brown !"
        },
        "_explanation" : {
          "value" : 1.0,
          "description" : "ConstantScore(full_text:foxes)",
          "details" : [ ]
        }
      }
    ]
  }
}

可以明显看到在 _explanation 中,使用constant score 进行 filter 的算分逻辑明显简单了很多。

 

3.2 terms

 

terms 查询与 term 查询其实是一样的,只是 terms 查询可以对一个字段同时查询多个词项。

相关使用如下:


GET term-query/_search
{
  "query": {
    "terms": {
      "user.id": [ "kimchy", "elkbee" ], 
      "boost": 1.0
    }
  }
}

terms lookup

 

Term lookup 是一种参照某个索引文档的某一字段内容去搜索拥有同样值的文档方法。它可以获取现有文档的字段值,然后 ES 使用这些值作为搜索词去 terms 搜索。

 

它有两个使用限制:


1、使用 terms lookup ,_source 设置为 enabled(默认是开启的)。

2、不能在跨集群搜索上进行 terms lookup。

 

使用方法:

GET _search?pretty
{
  "query": {
    "terms": {
        "color" : {
            "index" : "my-index-000001",
            "id" : "2", 
            "path" : "color" 
        }
    }
  }
}

其中 path 是被参照文档的具体字段,在一些对象字段或者 nested 字段中,可以以“field.subfield” 的形式查询。

 

实践:


# 创建 my-index-000001 并设置 color 字段属性为 keyword。
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "color": { "type": "keyword" }
    }
  }
}
# 创建测试文档,注意文档 3 与其它两个文档并没有内容交集。
PUT my-index-000001/_doc/1
{
  "color":   ["blue", "green"]
}
PUT my-index-000001/_doc/2
{
  "color":   "blue"
}
PUT my-index-000001/_doc/3
{
  "color":   "red"
}
# 测试,使用文档 2 进行 terms lookup 查询,理论上会将文档 1 和 2 返回。
GET my-index-000001/_search?pretty
{
  "query": {
    "terms": {
        "color" : {
            "index" : "my-index-000001",
            "id" : "2",
            "path" : "color"
        }
    }
  }
}
# 返回结果
{
  "took" : 24,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
"skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "color" : [
            "blue",
            "green"
          ]
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "color" : "blue"
        }
      }
    ]
  }
}



《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.3.全文搜索/精确搜索(5) https://developer.aliyun.com/article/1229939

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
传感器 监控
基于STM32的智能交通灯控制系统设计与实现
基于STM32的智能交通灯控制系统设计与实现
1803 0
|
存储 前端开发 JavaScript
【面试题】Promise只会概念远远不够,还需这17道题目巩固!
【面试题】Promise只会概念远远不够,还需这17道题目巩固!
338 0
|
资源调度
YARN集群启停命令
YARN集群启停命令
522 0
|
4月前
|
数据采集 算法 数据挖掘
大模型应用:从静态到动态:增量聚类+大模型破解无限流数据智能处理难题.98
本文详解“增量聚类 + 大模型”融合方案,破解无限流数据(如实时工单、舆情、日志)处理难题:增量聚类实现边流入边动态分组,大模型负责语义化打标(如“快递丢失理赔咨询”),替代人工标注。涵盖原理、StreamKM++算法、Embedding转换、Prompt工程及完整代码示例,助力企业构建实时智能分类系统。
411 3
|
C++ Windows
Windows7系统终级版,老旧电脑还能再用十年,赶紧珍藏!
老电脑跑不动Win11?推荐网友自制Win7“全补丁终极版”:集成2026年1月所有更新、Edge浏览器及VC++全运行库,含5个版本+35种语言包。提供11.8G完整版与4G精简版,轻量实用,驱动兼容性好,开箱即用!
583 0
Windows7系统终级版,老旧电脑还能再用十年,赶紧珍藏!
|
运维 Kubernetes Cloud Native
分钟级到秒级:Yahaha 基于 OpenKruiseGame 的 UE5 游戏云原生实践
回顾《STRIDEN》项目在短短两个月内完成云原生转型的历程,它验证了一条清晰、可行的路径,即如何利用云原生技术,从根本上解决现代在线游戏所面临的运维复杂性难题。
|
人工智能 大数据 测试技术
自主和开放并举 探索下一代阿里云AI基础设施固件创新
12月13日,固件产业技术创新联盟产业峰会在杭州举行,阿里云主导的开源固件测试平台发布和PCIe Switch固件技术亮相,成为会议焦点。
|
C语言 C++
STM32F103C8 串口的使用
STM32F103C8 串口的使用
854 0
|
网络安全 数据安全/隐私保护 网络虚拟化
|
人工智能 运维 监控
SLS 智能运维 AI 基础模型创新
SLS 全新发布运维场景基础模型,覆盖 Log、Metric、Trace 等可观测数据场景,模型提供开箱即用的异常检测、自动标注、分类和根因分析等能力;根因分析算法千级异常请求秒级定位,生产中准确率达95%;同时支持人工辅助微调,提供人工标注、结果打标修正,模型根据人工反馈自动微调,提升场景准确率。
92110 1

热门文章

最新文章