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

相关推荐
Xzaveir_77714 分钟前
企业号码负面标记治理:认证、申诉与合规事件的三轨模型
大数据·网络·人工智能·科技·产品经理
hnsoyon29 分钟前
智慧管廊综合管理平台赋能排水管网监测精细化管理
大数据·人工智能·智慧管廊综合管理平台·排水管网监测
数智化管理手记1 小时前
预算控制手工核对效率低?预算控制数字化落地实操如何做?
大数据·网络·数据库·人工智能·数据挖掘
千桐科技1 小时前
DataX 执行引擎正式加入:qData 开源版 v1.6.0 新增 Quartz + DataX 轻量运行模式!
java·大数据·开源·数据治理·数据中台·qdata
lupai1 小时前
汽车维保记录精准版 API 快速接入指南
大数据·人工智能·汽车
王同学的AI学习日记2 小时前
Qwen-Image-3.0技术实测:文字渲染、长上下文与多场景能力深度评测
大数据·人工智能·ai·开源
AI行业说2 小时前
美国布艺市场全品类生产解析——誉财YC-18-M8045四轴模板机的多品种柔性制造方案
大数据·人工智能·自动化·智能家居·自动化缝纫
晓子文集2 小时前
Tushare接口文档:现金流量表(cashflow)
大数据·数据库·大模型·金融数据·量化投资·tushare
威联通安全存储2 小时前
TS-h1677AXU-RP在工程机械机器人弧焊中的部署
大数据·人工智能·python·机器人
科技之门2 小时前
存量破局・价值新生 高格办公探寻办公资产全新增长曲线
大数据·人工智能