首页 > 其他分享 >Elasticsearch 全文搜索

Elasticsearch 全文搜索

时间:2024-10-09 19:32:54浏览次数:1  
标签:index brown title dog 搜索 query Elasticsearch 全文 match

全文搜索

  • match
    • operator 提高精度
    • minimum_should_match 控制精度
  • bool 组合查询
    • must 必须匹配
    • must_not 必须不匹配
    • should 如果有 must 则表示没有必须匹配但有会更匹配,如果没有 must 则表示至少需要有一个匹配
    • minimum_should_match 控制多少个 should 需要匹配
  • boost 控制权重

示例数据

curl -X DELETE -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index"
curl -X PUT -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index" -d'
{
    "settings": {
        "number_of_shards": 1
    }
}
'
curl -X POST -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_bulk" -d'
{ "index": { "_id": 1 }}
{ "title": "The quick brown fox" }
{ "index": { "_id": 2 }}
{ "title": "The quick brown fox jumps over the lazy dog" }
{ "index": { "_id": 3 }}
{ "title": "The quick brown fox jumps over the quick dog" }
{ "index": { "_id": 4 }}
{ "title": "Brown fox brown dog" }
'
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty"

单个词查询

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "match": {
            "title": "quick"
        }
    }
}
'

多个词查询

匹配 brown 或 dog

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "match": {
            "title": "brown dog"
        }
    }
}
'

提高精度

必须同时匹配 brown 和 dog (operator的默认值是 or)

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "match": {
            "title": {
                "query": "brown dog",
                "operator": "and"
            }
        }
    }
}
'

控制精度

minimum_should_match 支持数值,表示需要匹配的词项数;或者百分比值

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "match": {
            "title": {
                "query": "quick brown dog",
                "minimum_should_match": "75%"
            }
        }
    }
}
'

组合查询

必须包含 quick,必须不包含 lazy,如果包含 brown dog 相关度会更高

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "bool": {
            "must": {"match": {"title": "quick"}},
            "must_not": {"match": {"title": "lazy"}},
            "should": [
                {"match": {"title": "brown"}},
                {"match": {"title": "dog"}}
            ]
        }
    }
}
'

控制精度

minimum_should_match 至少有多少个 should 需要匹配

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "bool": {
            "should": [
                {"match": {"title": "brown"}},
                {"match": {"title": "fox"}},
                {"match": {"title": "dog"}}
            ],
            "minimum_should_match": 2
        }
    }
}
'

提升权重

必须包含 brown dog,如果有 quick fox 会更匹配

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": {
                        "query": "brown dog",
                        "operator": "and"
                    }
                }
            },
            "should": [
                {"match": {"title": "quick"}},
                {"match": {"title": "fox"}}
            ]
        }
    }
}
'

必须包含 brown dog,如果有 quick fox 会更匹配;quick 权重更高,fox 权重比默认高(默认为1)

boost == 1 默认,boost > 1 权重更高,boost > 0 and boost < 1 权重更低

curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": {
                        "query": "brown dog",
                        "operator": "and"
                    }
                }
            },
            "should": [
                {"match": {"title": {"query": "quick", "boost": 2}}},
                {"match": {"title": {"query": "fox", "boost": 1}}}
            ]
        }
    }
}
'

标签:index,brown,title,dog,搜索,query,Elasticsearch,全文,match
From: https://www.cnblogs.com/liaozibo/p/18441551

相关文章

  • Elasticsearch 结构化搜索
    过滤器当进行精确查找时,我们会使用过滤器。term单值匹配terms多值匹配bool复合过滤器(must/must_not/should)range范围查询(gt/lt/gtq/lte)existsnull值查询使用constant_score以非评分模式进行查询。示例数据curl-XDELETE-H'Content-Type:application/json......
  • 基于禁忌搜索算法的VRP问题求解matlab仿真,带GUI界面,可设置参数
    1.程序功能描述基于禁忌搜索算法的VRP问题求解matlab仿真,带GUI界面,可设置参数。2.测试软件版本以及运行结果展示MATLAB2022a版本运行 3.核心程序whileCOUNT<=ItertionsֲL=zeros(Ant_Num,1);fori=1:Ant_NumInfor_Tabu_tmps=......
  • ElasticSearch7.17.3简介+centos7详细安装教程+Springboot整合ES
    一、ElasticSearch简介    官方地址:Elasticsearch:官方分布式搜索和分析引擎|Elastic1.1ElasticSearch简介        Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,同时是可扩展的数据存储和矢量数据库,能够应对日益增多的各种用例。作为......
  • 6、Elasticsearch集群
    Elasticsearch集群1、三台机器大家集群192.168.204.209elasticsearch.ymlcluster.name:mycluster#集群名,所有节点node都应配置相同的集群名node.name:node1#本节点名,同一集群下不同的node名字不能重复node.master:truenode.data:truenetwork.host:0.0.0.0http.p......
  • 1、Elasticsearch安装
    Elasticsearch安装1.1什么是elasticsearch?ElasticSearch是一个分布式,高性能、高可用、可伸缩的搜索和分析系统。ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。Elasticsearch是用Java开发的,并作为Apache许可......
  • 【C++】二叉搜索树
    文章目录1、二叉搜索树的说明性2、二叉搜索树2.1二叉搜索树的概念2.2二叉搜索树的操作2.2.1插入2.2.2查找2.2.3删除2.3二叉搜索树的实现2.4二叉搜索树的应用二叉搜索树的性能分析1、二叉搜索树的说明性map和set特性需要先铺垫二叉搜索树,而二叉搜索树也是一种树形......
  • 二分搜索与二分答案
    二分前提条件数组的有序的数组数组中无重复元素,否则查找的元素下标可以不算唯一的二分答案二分答案时需要满足答案的有界性二分答案的思路:首先判断这个解是否为可行解,然后我们”认为“这个可行解的最优解,然后以这个可行解为标准去左(右)区间寻找最优解时间复杂......
  • 【电商搜索】现代工业级电商搜索技术-EMNLP2024-无监督的用户偏好学习
    【电商搜索】现代工业级电商搜索技术-EMNLP2024-无监督的用户偏好学习0.论文信息Title:UnsupervisedHumanPreferenceLearningAuthors:SumukShashidhar,AbhinavChinta,VaibhavSahai,DilekHakkaniTurComments:EMNLP2024MainConferencehttps://arxiv.or......
  • 逆向 Virustotal 搜索接口 X-VT-Anti-Abuse-Header
    搜索示例搜索123,网页地址为:https://www.virustotal.com/gui/search/123/comments请求接口GET/ui/search?limit=20&relationships%5Bcomment%5D=author%2Citem&query=123HTTP/1.1Accept-Encoding:gzip,deflate,br,zstdAccept-Ianguage:en-US,en;q=0.9,es;q=0.8Accep......
  • 3.搜索、模拟
    搜索、模拟\(A\)luoguP1120小木棍\(B\)luoguP2540[NOIP2015提高组]斗地主加强版\(C\)CF58EExpression\(D\)CF293BDistinctPaths\(E\)[ABC352F]EstimateOrder\(F\)[ABC336F]RotationPuzzle\(G\)CF525EAnyaandCubes\(H\)luoguP9234买瓜\(I\)......