仅返回elasticsearch查询中的源数据

时间:2015-11-07 15:40:17

标签: 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) {...

我在这里检查成功方法中的响应数据看起来如何: enter image description here

我只想点击哪些文件,在文档中我只想“_source”具有字段数据的对象。“take”,“shards”,“_ id”,“_ index”,不需要,我该怎么办?禁用它们

3 个答案:

答案 0 :(得分:6)

是的,您可以从_source

中删除多余的字段

答案只是一个简单的单词 filter_path

卷曲:

curl -XGET 'http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source'

节点:

如果您使用任何节点“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
        }
    }

在您的情况下:

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

"http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source&source=" + query

答案 1 :(得分:2)

您无法关闭响应元数据。如果您只需要特定字段,则可以返回fields而不是_source,但这并不会降低复杂性。该库可以抽象出一些,但我发现直接解析ES响应并不是非常困难。

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

var results = es_response_data['hits']['hits'].map(function(i){
    return i['_source']; 
});

答案 2 :(得分:1)

只需使用ElasticJS,ElasticSearch推荐的API将帮助JS客户端轻松地与elasticsearch节点进行通信。使用此API可以节省大量时间。

相关问题