前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch常用四种查询方式,你知道多少?

Elasticsearch常用四种查询方式,你知道多少?

作者头像
Tinywan
发布2024-08-05 14:30:55
780
发布2024-08-05 14:30:55
举报
文章被收录于专栏:开源技术小栈

匹配查询 Match

如果需要为不同字段设置不同权重,可以考虑使用 bool 查询的 should 子句来组合多个 match 查询,并为每个 match 查询设置不同的权重。

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "product_name": {
              "query": "apple",
              "boost": 3
            }
          }
        },
        {
          "match": {
            "description": {
              "query": "apple",
              "boost": 1
            }
          }
        }
      ]
    }
  }
}

上面的查询将在 product_name 字段和 description 字段中搜索包含 "apple" 的文档,并为 product_name 字段设置权重为 3,而为 description 字段设置权重为 1。这样,在计算匹配得分时,product_name 字段的匹配将比 description 字段的匹配更加重要,因为它的权重更高。这种方式可以灵活地控制不同字段的权重,以满足不同的搜索需求。

精确匹配查询 Match_pharse

match_phrase 查询是 Elasticsearch 中一种用于精确匹配短语的查询方式,可以确保查询字符串中的关键词按照给定的顺序在文档中连续出现。以下是 match_phrase 查询的用法

简单用法

match_phrase 查询可以直接指定一个字段和一个短语进行匹配。

代码语言:javascript
复制
$client = ElasticSearchCLient::getInstance();
$indexParams = [
    'index' => 'resty_product_test_index',
    'body' => [
        'query' => [
            'match_phrase' => [
                'title' => '开源技术小栈20245直播间'
            ]
        ]
    ]
];
$indexResponse = $client->search($indexParams);

查询结果

代码语言:javascript
复制
"hits": [
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "20245",
        "_score": 2.3340414,
        "_source": {
            "id": 20245,
            "title": "开源技术小栈20245直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    }
]

如果'title' => '开源技术小栈',则查询结果

代码语言:javascript
复制
"hits": [
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "2024",
        "_score": 1.0939294,
        "_source": {
            "id": 2024,
            "title": "开源技术小栈20240724直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20240724直播间"
        }
    },
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "20245",
        "_score": 1.0939294,
        "_source": {
            "id": 20245,
            "title": "开源技术小栈20245直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    }
]

位置信息

match_phrase 查询会记录匹配短语在文档中的位置信息,可以通过 slop 参数指定允许的位置偏移量。

代码语言:javascript
复制
$client = ElasticSearchCLient::getInstance();
$indexParams = [
    'index' => 'resty_product_test_index',
    'body' => [
        'query' => [
            'match_phrase' => [
                'title' => [
                    'query' => '开源技术小栈20245直播间',
                    'slop' => 2
                ]
            ]
        ]
    ]
];
$indexResponse = $client->search($indexParams);

多字段查询 Match multi_match

multi_match 查询可以直接指定一个查询字符串,然后在多个字段中进行搜索。

简单用法

代码语言:javascript
复制
$client = ElasticSearchCLient::getInstance();
$query = '开源技术小栈';
$indexParams = [
    'index' => 'resty_product_test_index',
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => $query,
                'fields' => ['title', 'content'],
            ]
        ]
    ]
];
$indexResponse = $client->search($indexParams);

查询结果

代码语言:javascript
复制
"hits": [
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "2024",
        "_score": 2.399356,
        "_source": {
            "id": 2024,
            "title": "开源技术小栈20240724直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20240724直播间"
        }
    },
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "20245",
        "_score": 2.399356,
        "_source": {
            "id": 20245,
            "title": "开源技术小栈20245直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    },
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "202457879",
        "_score": 1.1278633,
        "_source": {
            "id": 202457879,
            "title": "Tinywan",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    }
]

上面的查询将在titlecontent字段中搜索包含 开源技术小栈 的文档。

类型匹配

multi_match 查询可以通过 type 参数指定匹配的类型,如 "best_fields"、 "most_fields"、 "cross_fields"、 "phrase"、 "phrase_prefix" 等。不同的类型在匹配方式和结果计算上有所不同。

代码语言:javascript
复制
$client = ElasticSearchCLient::getInstance();
$query = '开发者社区';
$indexParams = [
    'index' => 'resty_product_test_index',
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => $query,
                'fields' => ['title', 'content'],
                "type" => "best_fields"
            ]
        ]
    ]
];
$indexResponse = $client->search($indexParams);
代码语言:javascript
复制
"hits": [
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "2024",
        "_score": 0.7439606,
        "_source": {
            "id": 2024,
            "title": "开源技术小栈20240724直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20240724直播间"
        }
    },
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "20245",
        "_score": 0.7439606,
        "_source": {
            "id": 20245,
            "title": "开源技术小栈20245直播间",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    },
    {
        "_index": "resty_product_test_index",
        "_type": "_doc",
        "_id": "202457879",
        "_score": 0.7439606,
        "_source": {
            "id": 202457879,
            "title": "Tinywan",
            "content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
        }
    }
]

上面的查询将使用 "best_fields" 类型在titlecontent字段中搜索包含短语开源技术小栈的文档。

高级搜索查询 query_string

在 Elasticsearch 中,query_string 是一种查询方式,用于在文本字段上执行灵活且强大的搜索操作。query_string 查询支持使用 Lucene 查询语法进行高级搜索,可以通过在查询字符串中指定不同的搜索条件、操作符和逻辑关系来构建复杂的搜索查询。

简单的关键词匹配

代码语言:javascript
复制
$client = ElasticSearchCLient::getInstance();
$query = '开发者社区';
$indexParams = [
    'index' => 'resty_product_test_index',
    'body' => [
        'query' => [
            'query_string' => [
                'default_field' => 'title',
                'query' => $query,
            ]
        ]
    ]
];
$indexResponse = $client->search($indexParams);

上面的查询将在 title字段中搜索包含关键词 "开发者社区" 的文档。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-08-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源技术小栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 匹配查询 Match
  • 精确匹配查询 Match_pharse
    • 简单用法
      • 位置信息
      • 多字段查询 Match multi_match
        • 简单用法
          • 类型匹配
          • 高级搜索查询 query_string
            • 简单的关键词匹配
            相关产品与服务
            云直播
            云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档