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

相关推荐
叮咚侠2 小时前
将已创建的Elasticsearch 8.12.0的docker容器中的数据挂载到宿主机操作步骤
运维·elasticsearch·docker·容器·kibana
老徐电商数据笔记2 小时前
技术复盘第三篇:百果园新零售核心业务流程主题域划分详解
大数据·数据仓库·零售·技术面试
TDengine (老段)2 小时前
TDengine IDMP 1.0.9.0 上线:数据建模、分析运行与可视化能力更新一览
大数据·数据库·物联网·ai·时序数据库·tdengine·涛思数据
云老大TG:@yunlaoda3602 小时前
如何使用华为云国际站代理商的BRS进行数据安全保障?
大数据·数据库·华为云·云计算
野生技术架构师2 小时前
SpringBoot+Elasticsearch实现高效全文搜索
spring boot·elasticsearch·jenkins
AI营销实验室2 小时前
原圈科技AI CRM系统创新模式深度解析,助力工业B2B企业转型
大数据·人工智能·科技
武子康2 小时前
大数据-189 Nginx JSON 日志接入 ELK:ZK+Kafka+Elasticsearch 7.3.0+Kibana 实战搭建
大数据·后端·elasticsearch
Fabarta技术团队2 小时前
枫清科技受邀参加CMIS 2025第六届中国医药华北数智峰会
大数据·人工智能·科技
小兜全糖(xdqt)2 小时前
.net 8 添加swagger以及批量index,批量删除 elasticsearch
elasticsearch·jenkins·.net