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

相关推荐
TDengine (老段)9 小时前
从施工监测到运营预警,桥科院用 TDengine 提升桥梁数据管理能力
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
宁波鹿语心理9 小时前
无条件的在场:一项极简亲子依恋修复练习的机制分析与实证观察
大数据
二哈赛车手15 小时前
新人笔记---ES和kibana启动问题以及一些常用的linux的错误排查方法,以及ES,数据库泄密解决方案[超详细]
java·linux·数据库·spring boot·笔记·elasticsearch
lizhihai_9916 小时前
股市学习心得—半导体12种核心材料
大数据·人工智能·学习
ZGi.ai17 小时前
智能客服系统设计:从工单分类到自动派单的工程实现
大数据·人工智能·分类
PaperData17 小时前
2000-2023年地级市数字基础设施评价指标体系
大数据·网络·数据库·人工智能·数据分析·经管
Blockchain Learning17 小时前
去中心化身份(DID)模型解析:区块链如何重塑身份管理?
大数据·去中心化·区块链
xcbrand18 小时前
政府事业机构品牌策划公司哪家可靠
大数据·人工智能·python
程序鉴定师18 小时前
如何选择合适的深圳小程序开发公司?
大数据·小程序
晨启AI19 小时前
GPT-5.5 来了!OpenAI 最新提示词指南深度解读
大数据·人工智能·ai·提示词