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

相关推荐
A__tao3 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
2501_948114244 小时前
2026年大模型API聚合平台技术评测:企业级接入层的治理演进与星链4SAPI架构观察
大数据·人工智能·gpt·架构·claude
黎阳之光4 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
专注API从业者5 小时前
淘宝商品详情 API 与爬虫技术的边界:合法接入与反爬策略的技术博弈
大数据·数据结构·数据库·爬虫
V搜xhliang02465 小时前
AI大模型在临床决策与手术机器人领域的应用
大数据·人工智能·机器人
A__tao5 小时前
Elasticsearch Mapping 一键生成 Proto 文件(支持嵌套 + 注释过滤)
大数据·elasticsearch·jenkins
Devin~Y5 小时前
高并发电商与AI智能客服场景下的Java面试实战:从Spring Boot到RAG与向量数据库落地
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
Gofarlic_OMS6 小时前
装备制造企业Fluent许可证成本分点典型案例
java·大数据·开发语言·人工智能·自动化·制造
程序员雷欧6 小时前
大模型应用开发学习第八天
大数据·人工智能·学习