如果简单地按一下api,则每个文档中有5个字段。但是我只想要这两个字段(user_id和loc_code),所以我在字段列表中提到了。但仍然会返回一些不必要的数据,例如_shards,hits,time_out等。

使用以下查询在chrome的 postman 插件中发出POST请求

<:9200>/myindex/mytype/_search
{
    "fields" : ["user_id", "loc_code"],
    "query":{"term":{"group_id":"1sd323s"}}
}

//输出
 {
        "took": 17,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 323,
            "max_score": 8.402096,
            "hits": [
                {
                    "_index": "myindex",
                    "_type": "mytype",
                    "_id": "<someid>",
                    "_score": 8.402096,
                    "fields": {
                        "user_id": [
                            "<someuserid>"
                        ],
                        "loc_code": [
                            768
                        ]
                    }
                },
               ...
            ]
        }
    }

但是我只想要文档字段(两个提到的字段),也不想要_id,_index,_type。有没有办法做到这一点

最佳答案

一个可能不完整但有很大帮助的解决方案是使用 filter_path 。例如,假设索引中包含以下内容:

PUT foods/_doc/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "chocolate cake", "calories": "too much" }
{ "index" : { "_id" : "2" } }
{ "name" : "lemon pie", "calories": "a lot!"  }
{ "index" : { "_id" : "3" } }
{ "name" : "pizza", "calories": "oh boy..."  }
像这样的搜索...
GET foods/_search
{
  "query": {
    "match_all": {}
  }
}
...将产生大量的元数据:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "lemon pie",
          "calories" : "a lot!"
        }
      },
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "chocolate cake",
          "calories" : "too much"
        }
      },
      {
        "_index" : "foods",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "pizza",
          "calories" : "oh boy..."
        }
      }
    ]
  }
}
但是,如果我们给搜索URL指定参数filter_path=hits.hits._score ...
GET foods/_search?filter_path=hits.hits._source
{
  "query": {
    "match_all": {}
  }
}
...它只会返回源(尽管仍然嵌套很深):
{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "name" : "lemon pie",
          "calories" : "a lot!"
        }
      },
      {
        "_source" : {
          "name" : "chocolate cake",
          "calories" : "too much"
        }
      },
      {
        "_source" : {
          "name" : "pizza",
          "calories" : "oh boy..."
        }
      }
    ]
  }
}
您甚至可以过滤字段:
GET foods/_search?filter_path=hits.hits._source.name
{
  "query": {
    "match_all": {}
  }
}
...您将得到:
{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "name" : "lemon pie"
        }
      },
      {
        "_source" : {
          "name" : "chocolate cake"
        }
      },
      {
        "_source" : {
          "name" : "pizza"
        }
      }
    ]
  }
}
如果愿意,您可以做更多的事情:只需检查documentation即可。

关于elasticsearch - 在Elasticsearch结果数据中排除_id和_index字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23968200/

10-11 09:17