【问题标题】:elasticsearch - only return specific fields without _source?elasticsearch - 只返回没有_source的特定字段?
【发布时间】:2020-04-08 23:35:14
【问题描述】:

我找到了一些答案,例如 Make elasticsearch only return certain fields?

但他们都需要_source字段。

在我的系统中,磁盘和网络都是稀缺资源。

我无法存储_source 字段,我不需要_index_score 字段。

ElasticSearch 版本:5.5

索引映射只是喜欢

{
  "index_2020-04-08": {
    "mappings": {
      "type1": {
        "_all": {
          "enabled": false
        },
        "_source": {
          "enabled": false
        },
        "properties": {
          "rank_score": {
            "type": "float"
          },
          "first_id": {
            "type": "keyword"
          },
          "second_id": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

我的查询:

GET index_2020-04-08/type1/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "first_id": "hello"
        }
      }
    }
  },
  "size": 1000,
  "sort": [
    {
      "rank_score": {
        "order": "desc"
      }
    }
  ]
}

我得到的搜索结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "index_2020-04-08",
        "_type": "type1",
        "_id": "id_1",
        "_score": null,
        "sort": [
          0.06621722
        ]
      },
      {
        "_index": "index_2020-04-08",
        "_type": "type1",
        "_id": "id_2",
        "_score": null,
        "sort": [
          0.07864579
        ]
      }
    ]
  }
}

我想要的结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_id": "id_1"
      },
      {
        "_id": "id_2"
      }
    ]
  }
}

我可以实现吗?

【问题讨论】:

  • 请提供ES版本。可以肯定的是,您没有将 _source 存储在 ES 文档中?可以分享你的映射吗?你在使用存储字段吗?
  • @Alexandre Juma 感谢您的关注。我提供了更多信息,请看一下。
  • 您也可以发布您的 API 调用吗?

标签: elasticsearch elasticsearch-query


【解决方案1】:

要返回文档中的特定字段,您必须执行以下两项之一:

  1. 在您的文档中包含_source 字段,该字段默认启用。
  2. 使用必须手动启用的stored fields 功能存储特定字段

因为您几乎需要文档 ID 和一些元数据,所以您可以使用 filter_path 功能。

这是一个接近您想要的示例(只需更改字段列表):

$ curl -X GET "localhost:9200/metricbeat-7.6.1-2020.04.02-000002/_search?filter_path=took,timed_out,_shards,hits.total,hits.max_score,hits.hits._id&pretty"
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_id" : "8SEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "8iEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "8yEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "9CEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "9SEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "9iEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "9yEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "-CEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "-SEGSHEBzNscjCyQ18cg"
      },
      {
        "_id" : "-iEGSHEBzNscjCyQ18cg"
      }
    ]
  }
}

【讨论】:

  • 返回:invalid version format: Q=ELASTICSEARCH&FILTER_PATH=TOOK,HITS.HITS._ID HTTP/1.1
  • 这似乎是正确的答案。但它在我的身上不起作用。
  • 我没有 5.5 的设置,但您能否发布引发该错误的完整 api 调用?
  • 现在运行良好,但有一个错字。非常感谢@Alexandre Juma
【解决方案2】:

只是为了根据您链接的 SO 问题进行澄清——您不是存储_source,而是向 ES 请求它。它通常用于限制您想要检索的内容,即

...
"_source": ["only", "fields", "I", "need"]
...

_score_index 等是无论如何都会被检索的元字段。您可以通过将大小设置为 0 并聚合来稍微“破解”它,即

{
  "size": 0,
  "aggs": {
    "by_ids": {
      "terms": {
        "field": "_id"
      }
    }
  }
} 

这将为您节省几个字节

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "terms" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Ac76WXEBnteqn982smh_",
          "doc_count" : 1
        },
        {
          "key" : "As77WXEBnteqn982EGgq",
          "doc_count" : 1
        }
      ]
    }
  }
}

但执行聚合有其自身的成本。

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 2015-06-11
    • 2018-10-12
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多