elasticsearch多字段组合查询示例

之前探索了elasticsearch如何插入数据和检索。

https://blog.csdn.net/liliang199/article/details/155594517

https://blog.csdn.net/liliang199/article/details/155574706

这里进一步探索多字段的组合查询,同时涉及精确查询和模糊查询。

所用示例参考和修改自网络资料。

1 数据准备

在实际进行组合查询前,先连接es,然后创建索引,导入数据。

1.1 连接es

这里假设ES已经本地安装,连接es的示例如下所示。

复制代码
from elasticsearch.helpers import bulk
import elasticsearch


class ElasticSearchClient(object):
    @staticmethod
    def get_es_servers():
        es_host = "http://localhost:9200"
        es_client = elasticsearch.Elasticsearch(hosts=es_host)
        return es_client

es_client = ElasticSearchClient().get_es_servers()
print(es_client.info())

输出示例如下,说明ES连接成功。

{'name': 'a2e27d00bb95', 'cluster_name': 'docker-cluster', 'cluster_uuid': 'fXhGBstXTKmI3dd0JBq_mw', 'version': {'number': '8.11.3', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '64cf052f3b56b1fd4449f5454cb88aca7e739d9a', 'build_date': '2023-12-08T11:33:53.634979452Z', 'build_snapshot': False, 'lucene_version': '9.8.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}

1.2 创建索引

这里示例创建和修改索引。

首先手贱名称为es_multi_field_v1的索引,包含document_id、title等字段。

复制代码
index = "es_multi_field_v1"

mapping = {
    "properties": {
        "document_id": {"type": "keyword", "store": True, "similarity": "boolean"},
        "title": {
            "type": "text"
        },
    }
}

print(es_client.indices.exists(index=index))

res = es_client.indices.create(
    index=index,
    mappings=mapping
)

print(res)

输出如下

False

{'acknowledged': True, 'shards_acknowledged': True, 'index': 'es_multi_field_v1'}

这里进一步添加名称为content的字段。

复制代码
# 添加新字段 "content" 为 text 类型
es_client.indices.put_mapping(
    index=index,
    body={
        "properties": {
            "content": {"type": "text"}
        }
    }
)
# 查看修改后的索引
res2 = es_client.indices.get(index=index)
print(res2)

输出显示,content字段已添加。

{'es_multi_field_v1': {'aliases': {}, 'mappings': {'properties': {'content': {'type': 'text'}, 'document_id': {'type': 'keyword', 'store': True, 'similarity': 'boolean'}, 'title': {'type': 'text'}}}, 'settings': {'index': {'routing': {'allocation': {'include': {'_tier_preference': 'data_content'}}}, 'number_of_shards': '1', 'provided_name': 'es_multi_field_v1', 'creation_date': '1766115482930', 'number_of_replicas': '1', 'uuid': 'cmSk_a4FT2e4VPoiJFUPsw', 'version': {'created': '8500003'}}}}}

1.3 导入数据

这里测试多次分批导入数据。

首先导入两个数据记录

复制代码
obj1 = {
        "document_id": "news_1",
        "title": u"The Ten Best Science Books of 2025",
        "content": u"In 2025, our science reporters followed the first confirmed glimpse of a colossal squid and a rare look at dinosaur blood vessels. We watched the odds of a future asteroid impact climb to higher-than-normal levels---then drop back down to zero. We parsed headlines on a blood test to detect cancer and a beloved pair of coyotes in New York City's Central Park. Throughout it all, many of us read extended works of science nonfiction, pulling back the curtain on tuberculosis, evolution and the Arctic....",
    }

obj2 = {
        "document_id": "news_2",
        "title": u"The 7 Most Groundbreakdddding NASA Discoveries of 2025",
        "content": u"In 2025, NASA fdddaced unprecedented uncertainty as it grappled with sweeping layoffs, looming budget cuts, and leadership switch-ups. Despite all of that, the agency somehow still managed to do some seriously astonishing science.....",
    }
_id1 = 1
es_client.index(index=index, body=obj1, id=_id1)
_id2 = 2
es_client.index(index=index, body=obj2, id=_id2)

输出如下

ObjectApiResponse({'_index': 'es_multi_field_v1', '_id': '2', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1})

进一步导入数据

复制代码
obj1 = {
        "document_id": "ndddews_1",
        "title": u"The Tenddd Best Science Books of 2025",
        "content": u"In 2025dddd, our science reporters followed the first confirmed glimpse of a colossal squid and a rare look at dinosaur blood vessels. We watched the odds of a future asteroid impact climb to higher-than-normal levels---then drop back down to zero. We parsed headlines on a blood test to detect cancer and a beloved pair of coyotes in New York City's Central Park. Throughout it all, many of us read extended works of science nonfiction, pulling back the curtain on tuberculosis, evolution and the Arctic....",
    }
res = es_client.index(index=index, body=obj1)

_id = res["_id"]
_id

输出如下,每次导入数据,均会返回对应数据的唯一"_id"。

'09iwNJsBHbZWxqnUIebO'

2 组合查询

这里进一步示例组合。

2.1 组合查询逻辑

这里同时对document_id和title字段的查询。

对document_id的查询需要精确匹配,所以采用term方式

对title字段采用关键词匹配,所以采用match方式。

由于,需要同时满足document_id的精确匹配和titlte的match匹配,所以采用must方式组合。

2.2 有效组合查询

结合以上组合逻辑,query示例如下,其中

document_id为news_2

match条件为"NASA intensive Discoveries",不完全匹配title,但NASA和Discoveries能匹配。

复制代码
query = {
  "query": {
      "bool": {
          "must": [
              {"term": {"document_id": "news_2"}},
              {"match": {"title": "NASA intensive Discoveries"}}
          ]
      }
  }
}
res = es_client.search(index=index, body=query)
print(res)

运行查询,输出如下所示,精确匹配出_id为2的elasticsearch记录。

{'took': 28, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 1, 'relation': 'eq'}, 'max_score': 2.89132, 'hits': {'_index': 'es_multi_field_v1', '_id': '2', '_score': 2.89132, '_source': {'document_id': 'news_2', 'title': 'The 7 Most Groundbreakdddding NASA Discoveries of 2025', 'content': 'In 2025, NASA fdddaced unprecedented uncertainty as it grappled with sweeping layoffs, looming budget cuts, and leadership switch-ups. Despite all of that, the agency somehow still managed to do some seriously astonishing science.....'}}}}

2.3 无效组合查询

这里通过给document_id的字段添加字符方式,模拟document_id不完全匹配。

具体为:

正确document_id: "news_2"

修改后document_id: "news_2x"

title的匹配条件不修改,同上。

代码示例如下,由于must的一个查询条件不满足,应该匹配不到elasticsearch记录。

复制代码
query = {
  "query": {
      "bool": {
          "must": [
              {"term": {"document_id": "news_2x"}},
              {"match": {"title": "NASA intensive Discoveries"}}
          ]
      }
  }
}
res = es_client.search(index=index, body=query)
print(res)

运行查询,输出如下所示,hits显示没有匹配到记录,符合预期。

{'took': 7, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': \[\]}}

reference


elasticsearch全文搜索索引结构示例

https://blog.csdn.net/liliang199/article/details/155594517

elasticsearch增删改查索引结构示例

https://blog.csdn.net/liliang199/article/details/155574706

Elasticsearch 根据两个字段搜索(包括 multi-match 查询、bool 查询和查询时字段加权)

https://zhuanlan.zhihu.com/p/1916800962582012327

相关推荐
Q264336502311 分钟前
【有源码】基于文本挖掘的消费者金融投诉主题发现与风险预警研究,基于Python的CFPB消费者投诉数据可视化分析系统
大数据·hadoop·机器学习·数据挖掘·spark·毕业设计·课程设计
南京兴帝文化传媒有限公司13 分钟前
本地生活服务GEO优化案例:宁国瑜伽馆地图关键词布局与AI问答优化路径
大数据·人工智能·生活·geo 优化·ai搜索获客
数字新视界24 分钟前
国产化动环技术提升数据中心监控效率与安全性
大数据·人工智能·数据中心·微模块机房·模块化机房
isfox37 分钟前
MapReduce OutputFormat 解析:结果怎么从键值对变成文件
大数据
林澈在路上1 小时前
能做DJ的AI写歌软件推荐:MELO音乐对比Suno v6
大数据·人工智能·github·音视频·音频
小羊没烦恼!1 小时前
Memory 记忆设计讨论:Agent Memory 的安全边界:权限、删除、版本与回执
java·大数据·word·powerpoint·.net
喻师傅2 小时前
Apache Hudi 概述:从 Parquet 更新难题到数据湖 Upsert
大数据·数据仓库·big data
qq29532 小时前
自然语言投研 Agent 的工程化选型:从问财式 NL2SQL 到多智能体投研社区
大数据·人工智能
一隅论数智2 小时前
数据消费主体变了:智能时代如何重构业务管理逻辑
大数据·人工智能·经验分享·笔记·学习·行业数字化
时下握今2 小时前
战略数据分析完整流程:从桌面研究、调研取证到报告落地
大数据·人工智能