python - 弹性搜索 - 带有字段映射的多个索引
问题描述
我需要搜索多个索引及其字段映射。
例如我想查询一个字符串,在
field1 with index1 field2 with index2
from elasticsearch import Elasticsearch
es = Elasticsearch([eshost])
req_string = {
"size":1000,
"query": {
"query_string": {
"query": "string to be searched",
"fields": ["field1","field2"],
}
}
}
res = es.search(index='index1,index2', body=req_string)
有可能吗?如果是,请提供一些链接。提前致谢 !
解决方案
跨多个索引查询时,您可以使用_index 字段。
_index 字段允许匹配文档被索引到的索引。它的值可在某些查询和聚合中以及在排序或编写脚本时访问
添加带有索引数据、搜索查询和搜索结果的工作示例。
指数数据:
PUT/ index1/_doc/1
{
"name": "Hello"
}
PUT/ index2/_doc/1
{
"name": "Hello World"
}
搜索查询:
{
"query": {
"bool": {
"filter": [
{
"terms": {
"_index": [
"index1",
"index2"
]
}
}
],
"must": [
{
"simple_query_string": {
"query": "hello",
"fields": [
"name",
"title"
]
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "index2",
"_type": "_doc",
"_id": "1",
"_score": 0.4700036,
"_source": {
"name": "Hello World"
}
},
{
"_index": "index1",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"title": "Hello"
}
}
]
更新的搜索查询:
下面的搜索查询将搜索仅在中的title
字段index1
和name
仅在中的字段index2
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": [
{
"terms": {
"_index": [
"index1"
]
}
}
],
"must": [
{
"query_string": {
"query": "hello",
"fields": [
"title"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"terms": {
"_index": [
"index2"
]
}
}
],
"must": [
{
"query_string": {
"query": "hello",
"fields": [
"name"
]
}
}
]
}
}
]
}
}
}
推荐阅读
- python-3.x - RandomForestRegressor 仅吐出 1 个预测
- java - Oracle Java 教程 [基本类 > 基本 I/O > 文件操作] 和 java.nio.file.Files 类 javadocs 差异
- r - R根据名称比较特定列
- r - 如何绘制历史数据和未来预测,以及预测的错误阴影
- android - 可在 Sceneform 和 ARCore 中渲染的 ShapeFactory 的不可见/透明材质
- python - 使用 Python randomQuizGenerator 错误自动化无聊的事情
- tensorflow - Exactly match 2 rows in a tensor Tensorflow
- r - 将 setdiff 结果存储在数据框单元格中
- java - 为什么我的对象没有被添加到我的数组中?
- c++ - 同时运行两个函数。(即时的)