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

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 带你读《Elastic Stack 实战手册》之34:——3.4.2.17.3.全文搜索/精确搜索(12)

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



现在进行 match_phrase 查询 "this is test"。

 

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is test",
        "slop": 0
      }
    }
  }
}
# 返回结果只有文档 1
{
  ······
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.26582208,
        "_source" : {
          "message" : "this is test"
        }
      }
    ]
  }
}

在这个查询中,被匹配返回的文档 1 符合了下面的条件:

 

1、this、is、test 三个词项都出现在文档中;

2、is 的位置比 test 的位置大 1 ,两者的 slop 为 0;

3、test 的位置比 is 的位置大 1 。

# 将 slop 设置为 1,词项之间的位置差小于等于 2 即可匹配返回。 
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is test",
        "slop": 1
      }
    }
  }
}
# 返回文档 1 和 2
{
  ······
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.26582208,
        "_source" : {
          "message" : "this is test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.16089231,
        "_source" : {
          "message" : "this is  a test"
        }
      }
    ]
  }
}
# 将 slop 参数设置为4 
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is test",
        "slop": 4
      }
    }
  }
}
# 返回了全部 5 个文档
{
  ······
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.26582208,
        "_source" : {
          "message" : "this is test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.16089231,
        "_source" : {
          "message" : "this is  a test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.106328845,
        "_source" : {
          "message" : "this is  not a test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.07501727,
        "_source" : {
          "message" : "this a is  not a test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.055580944,
        "_source" : {
          "message" : "this a  or is  not a test"
        }
      }
    ]
  }
}

对于词项换位( transposed terms )的情况,即则 slop 需要设置成原词项距离加 2 。

 

比如下面的查询:


PUT my-index-000001
{"mappings":{"properties":{"message":{"type":"text"}}}}
POST my-index-000001/_bulk
{ "index": { "_id": 1 }}
{ "message": "test this" }
{ "index": { "_id": 2 }}
{ "message": "this test" }
{ "index": { "_id": 3 }}
{ "message": "test in this" }

想要使用 ”this test“ 匹配到文档 1,则需要将 slop 设置为 2 ;而想匹配到文档 3 ,则需要将 slop 设置为 3.

 

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this test",
        "slop": 2
      }
    }
  }
}
# 查询结果为文档 1 和 2
{
  ......
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.28363907,
        "_source" : {
          "message" : "this test"
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13941583,
        "_source" : {
          "message" : "test this"
        }
      }
    ]
  }
}
GET my-index-000001/_search
{
  "query": {
"match_phrase": {
      "message": {
        "query": "this test",
        "slop": 3
      }
    }
  }
}
# 查询结果中有文档 3 
{
  ......
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.08604115,
        "_source" : {
          "message" : "test in this"
        }
      }
    ]
  }
}

match_phrase 的查询结果也随着分词器的不同而变化。

 


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

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
部署hexo后样式丢失问题
部署hexo后样式丢失问题
225 0
|
消息中间件 存储 网络协议
ZMQ/ZeroMQ简介
ZMQ/ZeroMQ简介
|
Java 关系型数据库 MySQL
高校大学生社团管理系统的设计与实现(论文+源码)_kaic
高校大学生社团管理系统的设计与实现(论文+源码)_kaic
|
5月前
|
监控 Java Go
无感改造,完美监控:Docker 多阶段构建 Go 应用无侵入观测
本文将介绍一种基于 Docker 多阶段构建的无侵入 Golang 应用观测方法,通过此方法用户无需对 Golang 应用源代码或者编译指令做任何改造,即可零成本为 Golang 应用注入可观测能力。
299 85
|
7月前
|
Linux API
Linux下载工具wget与curl
`wget` 是一个用于从网络下载文件的命令行工具,支持HTTP、HTTPS和FTP协议。它能自动处理下载中断,并支持递归下载网站内容。基本用法:`wget URL`,可指定文件名(`-O`)、保存目录(`-P`),还支持断点续传(`-c`)、限速(`--limit-rate`)和递归下载(`-r`)。相比之下,`curl` 更侧重于发送各种HTTP请求(如GET、POST),并支持文件上传、自定义请求头和cookie等功能。
179 10
|
9月前
|
机器学习/深度学习 分布式计算 Java
《探索 Apache Spark MLlib 与 Java 结合的卓越之道》
本文探讨了Apache Spark MLlib与Java结合的最佳实践,涵盖基础认知、数据预处理、模型选择与构建、训练调优及部署应用。Spark以其分布式计算能力著称,MLlib提供丰富的机器学习算法,Java则拥有成熟生态。两者结合可高效处理大规模数据集,构建灵活的机器学习应用。通过RDD和DataFrame API进行数据操作,利用特征工程工具优化数据,选择合适的分类、回归或聚类模型,并通过管道机制简化工作流。模型训练时合理设置参数并调优,最终将模型部署到生产环境,释放其商业价值。
151 8
|
10月前
|
前端开发 C# Windows
在WPF程序中实现PropertyGrid功能
【11月更文挑战第15天】PropertyGrid 是一个用户界面组件,用于直观地查看和编辑对象属性。在 WPF 中可通过组合 Expander 和 DataGrid 实现基本功能,或使用第三方库 PropertyTools 获得更强大特性,包括属性验证和类型特定编辑器。
620 3
13 MFC - 静态文本框CStatic
13 MFC - 静态文本框CStatic
169 0
【已解决】Pygame无法显示中文
【已解决】Pygame无法显示中文
|
编解码 前端开发 智能网卡
【LC3开源峰会网络技术系列之二】阿里云开发智能网卡的动机、功能框架和软转发程序
摘要 这篇文章介绍了阿里云开发智能网卡的动机、功能框架和软转发程序以及在软转发过程中发现的问题和优化方法。 主讲人陈静 阿里云高级技术专家 主题Zero-copy Optimization for DPDK vhost-user Receiving 分论坛Network & Orchestration 项目背景 在VPC产品部署中虚拟交换Virtual Switch承担着overlay层和underlay层进行网络协议的加解密encap/decap功能在多租户虚拟机或者容器的主机上也需要进行二三层的路由转发、Qos、限流、安全组等。
5712 0