簡體   English   中英

elasticsearch - 只返回沒有_source的特定字段?

[英]elasticsearch - only return specific fields without _source?

我找到了一些答案,例如Make elasticsearch 只返回某些字段?

但他們都需要_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"
      }
    ]
  }
}

我可以實施嗎?

要返回文檔中的特定字段,您必須執行以下兩項之一:

  1. 在您的文檔中包含_source字段,默認情況下啟用。
  2. 使用必須手動啟用的存儲字段功能存儲特定字段

因為您幾乎需要文檔 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"
      }
    ]
  }
}

只是為了根據您鏈接的 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
        }
      ]
    }
  }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM