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

简介: 带你读《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并实现搜索。  
相关文章
|
8月前
|
存储 Ubuntu Linux
容器化部署引擎Docker
本节介绍Docker技术,解决微服务部署中依赖兼容与环境差异问题。通过镜像打包应用及依赖,容器隔离运行,实现跨环境一致部署。对比虚拟机,Docker更轻量、高效。涵盖镜像、容器、仓库概念及Docker架构,并演示CentOS下安装配置流程。
|
SQL XML Java
MyBatis Mapper中使用limit参数的查询问题
总结而言,MyBatis中使用 `limit`参数的查询可以高度定制并且灵活,基于方法签名和XML映射文件的组合来达成多样化的查询需求。通过参数化查询和动态SQL,MyBatis可以有效地处理各种复杂情境下的数据库操作,并且将SQL语句的维护与业务代码的编写相分离,提升代码的可维护性和可阅读性。
898 13
|
搜索推荐 网络架构 UED
什么是超链接?
本文介绍超链接的基本概念及其在网络中的重要性。超链接作为Web的核心组成部分,由蒂姆·伯纳斯·李在1989年提出,与URL、HTTP共同构成Web的三大支柱。通过链接,用户能轻松从一个文档跳转至另一个文档。文章探讨了不同类型的链接,包括内部链接、外部链接与传入链接,并介绍了锚点链接的功能。此外,还强调了链接对于用户体验及搜索引擎优化(SEO)的重要性,以及如何合理构建链接以提升网站的可用性和搜索引擎排名。最后,提供了进一步学习资源,帮助读者深入了解URL结构与超链接的实际应用。
2122 3
|
NoSQL Java Redis
Spring Boot 自动配置机制:从原理到自定义
Spring Boot 的自动配置机制通过 `spring.factories` 文件和 `@EnableAutoConfiguration` 注解,根据类路径中的依赖和条件注解自动配置所需的 Bean,大大简化了开发过程。本文深入探讨了自动配置的原理、条件化配置、自定义自动配置以及实际应用案例,帮助开发者更好地理解和利用这一强大特性。
2566 15
从代码中感悟人生:一段编程旅程的启示
在编码的世界里,每一行代码都像是生命中的一次选择,每一个函数都承载着特定的使命。本文以编程的视角,探讨技术成长与人生哲理之间的奇妙联系。从最初的迷茫到逐渐找到方向,再到不断学习和提升,这段旅程充满了挑战与收获。正如甘地所言:“你必须成为你希望在世界上看到的改变。”通过编程,我们不仅塑造了软件,也塑造了自己。
|
人工智能 Cloud Native Java
云原生技术深度解析:从IO优化到AI处理
【10月更文挑战第24天】在当今数字化时代,云计算已经成为企业IT架构的核心。云原生作为云计算的最新演进形态,旨在通过一系列先进的技术和实践,帮助企业构建高效、弹性、可观测的应用系统。本文将从IO优化、key问题解决、多线程意义以及AI处理等多个维度,深入探讨云原生技术的内涵与外延,并结合Java和AI技术给出相应的示例。
493 1
|
Java Android开发
Eclipse启动时指定jdk版本
Eclipse启动时指定jdk版本
838 0
解决 kali换源之后签名无效
解决 kali换源之后签名无效
1898 0
|
JavaScript 前端开发
【前端】学习VUE框架之运行环境node.js安装步骤
前面了解了vue的一些概念知识点,接下来开始安装vue运行环境了,它需要安装node.js,接下来接介绍如何安装node.js
496 0
【前端】学习VUE框架之运行环境node.js安装步骤

热门文章

最新文章