仅返回elasticsearch查询中的源数据

12 elasticsearch

我发布一个查询到elasticsearch获取索引的数据..我只需要这些字段数据和有多少文件找到信息......但是有"take","shards"和文档里面的"_id","_ index", "_得分了".这些对我来说是不必要的..

这是我的简单要求:

query='{"query": {"match_all": {}}}';
 $.ajax({
        url: "http://localhost:9200/webproxylog/_search?source=" + query,
        type:"GET",
        dataType: "json",
        data: $.param(params),
        success: function(data) {...
Run Code Online (Sandbox Code Playgroud)

我在这里检查成功方法中的响应数据看起来如何: 在此输入图像描述

我只是想点击哪些文档,在文档中我只想"_source"具有字段数据的对象."take","shards","_ id","_ index",不必要,我该如何禁用它们

Viv*_*shi 7

是的,您可以从_source中删除多余的字段

答案只是一个简单的词 filter_path

卷曲度:

curl -XGET 'http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source'
Run Code Online (Sandbox Code Playgroud)

节点:

如果使用任何节点' elasticsearch',则只需添加一个extrs参数filterPath

client.search({
        index: 'index',
        type: 'type',
        filterPath : ['hits.hits._source'], // this will remove extra fileds _index / _score / _id ...
        body: {
            sort: [
                {"posted_dt": {"order": "desc"}},
                "_score"
            ],
            query: query,
            size: 50,
            from: index * 50
        }
    }
Run Code Online (Sandbox Code Playgroud)

在您的情况下:

您只需要在网址中添加该额外字段,例如:

"http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source&source=" + query
Run Code Online (Sandbox Code Playgroud)


Slo*_*ens 6

您无法关闭响应元数据。如果您只需要特定字段,则可以使用 returnfields代替_source,但这并不能真正降低复杂性。该库可以抽象出其中的一些内容,但我发现直接简单地解析 ES 响应并不是非常困难。

当然,您必须编写一些 JavaScript。幸运的是,这并不太难。像这样的事情通常对我来说效果很好:

var results = es_response_data['hits']['hits'].map(function(i){
    return i['_source']; 
});
Run Code Online (Sandbox Code Playgroud)