如何将弹性搜索的返回json搜索结果过滤到我的文档中

时间:2017-10-31 22:39:13

标签: elasticsearch elasticsearch-plugin elasticsearch-5

我正在使用AWS弹性搜索和以下文档来检索我的数据:

elastic search

具体而言我在javascript中使用msearch如下:

function searchElasticSearchDocument(callback) {
client.msearch({
    body: [
        // match all query, on all indices and types
        {},
        { query: { match_all: {} } },

        // query_string query, on index/mytype
        { _index: 'story', _type: 'mytypeee' },
        { query: { query_string: { query: '"this is click tj test"' } } }
    ]
}, function(error, response){
    console.log("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
    console.log(JSON.stringify(response).replace(/\\/g, ''));
    console.log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
    console.log(JSON.stringify(response));
});

}

我可以看到所有结果如下:

{
"responses": [
    {
        "took": 12,
        "timed_out": false,
        "_shards": {
            "total": 21,
            "successful": 21,
            "failed": 0
        },
        "hits": {
            "total": 24,
            "max_score": 1,
            "hits": [
                {
                    "_index": ".kibana",
                    "_type": "index-pattern",
                    "_id": "test-2222222",
                    "_score": 1,
                    "_source": {
                        "title": "test-2222222",
                        "notExpandable": true,
                        "fields": "[{\"name\":\"_id\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_score\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_source\",\"type\":\"_source\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"title.keyword\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]"
                    }
                },
                {
                    "_index": ".kibana",
                    "_type": "config",
                    "_id": "5.5.2",
                    "_score": 1,
                    "_source": {
                        "buildNum": 15443,
                        "defaultIndex": "test-2222222"
                    }
                },
                {
                    "_index": ".kibana",
                    "_type": "index-pattern",
                    "_id": "myindex444",
                    "_score": 1,
                    "_source": {
                        "title": "myindex444",
                        "notExpandable": true,
                        "fields": "[{\"name\":\"_id\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_score\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_source\",\"type\":\"_source\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"title.keyword\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]"
                    }
                },
                {
                    "_index": ".kibana",
                    "_type": "index-pattern",
                    "_id": "333444",
                    "_score": 1,
                    "_source": {
                        "title": "333444",
                        "notExpandable": true,
                        "fields": "[{\"name\":\"_id\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_score\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_source\",\"type\":\"_source\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"title.keyword\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]"
                    }
                },
                {
                    "_index": ".kibana",
                    "_type": "index-pattern",
                    "_id": "story",
                    "_score": 1,
                    "_source": {
                        "title": "story",
                        "notExpandable": true,
                        "fields": "[{\"name\":\"_id\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_score\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_source\",\"type\":\"_source\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"title.keyword\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]"
                    }
                },
                {
                    "_index": "333444",
                    "_type": "mytypeee",
                    "_id": "22",
                    "_score": 1,
                    "_source": {
                        "title": "mazdammm"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "2000",
                    "_score": 1,
                    "_source": {
                        "title": "this is click tj test. initial phase."
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3121",
                    "_score": 1,
                    "_source": {
                        "channel_name": "qqqqqqqqthis is click tj test. initial phase.333333333333333"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3140",
                    "_score": 1,
                    "_source": {
                        "category_id": "777",
                        "channel_id": "111",
                        "channel_name": "uuuuu  yyyyyy tttttt",
                        "owner": "tjtest",
                        "owner_channel": "tjtest",
                        "byline_name": "tjtest",
                        "byline_publication": "tjtest",
                        "headline": "tjtest",
                        "publish_datetime": "tjtest",
                        "seo_terms": "tjtest",
                        "create_datetime": "tjtest",
                        "modify_datetime": "tjtest",
                        "enabled": "3",
                        "video_url": "tjtest",
                        "cms_source_id": "3"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3143",
                    "_score": 1,
                    "_source": {
                        "category_id": "777",
                        "channel_id": "111",
                        "channel_name": "uuuuu  yyyyyy ttttttfffff",
                        "owner": "tjtest",
                        "owner_channel": "tjtest",
                        "byline_name": "tjtest",
                        "byline_publication": "tjtest",
                        "headline": "tjtest",
                        "publish_datetime": "tjtest",
                        "seo_terms": "tjtest",
                        "create_datetime": "tjtest",
                        "modify_datetime": "tjtest",
                        "enabled": "3",
                        "video_url": "tjtest",
                        "cms_source_id": "3"
                    }
                }
            ]
        },
        "status": 200
    },
    {
        "took": 8,
        "timed_out": false,
        "_shards": {
            "total": 21,
            "successful": 21,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 4.8003736,
            "hits": [
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "2000",
                    "_score": 4.8003736,
                    "_source": {
                        "title": "this is click tj test. initial phase."
                    }
                }
            ]
        },
        "status": 200
    }
]

}

但是因为你很难分开并获得所需的文件。我只需要这个部分:

 {
                    "_index": "333444",
                    "_type": "mytypeee",
                    "_id": "22",
                    "_score": 1,
                    "_source": {
                        "title": "mazdammm"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "2000",
                    "_score": 1,
                    "_source": {
                        "title": "this is click tj test. initial phase."
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3121",
                    "_score": 1,
                    "_source": {
                        "channel_name": "qqqqqqqqthis is click tj test. initial phase.333333333333333"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3140",
                    "_score": 1,
                    "_source": {
                        "category_id": "777",
                        "channel_id": "111",
                        "channel_name": "uuuuu  yyyyyy tttttt",
                        "owner": "tjtest",
                        "owner_channel": "tjtest",
                        "byline_name": "tjtest",
                        "byline_publication": "tjtest",
                        "headline": "tjtest",
                        "publish_datetime": "tjtest",
                        "seo_terms": "tjtest",
                        "create_datetime": "tjtest",
                        "modify_datetime": "tjtest",
                        "enabled": "3",
                        "video_url": "tjtest",
                        "cms_source_id": "3"
                    }
                },
                {
                    "_index": "story",
                    "_type": "mytypeee",
                    "_id": "3143",
                    "_score": 1,
                    "_source": {
                        "category_id": "777",
                        "channel_id": "111",
                        "channel_name": "uuuuu  yyyyyy ttttttfffff",
                        "owner": "tjtest",
                        "owner_channel": "tjtest",
                        "byline_name": "tjtest",
                        "byline_publication": "tjtest",
                        "headline": "tjtest",
                        "publish_datetime": "tjtest",
                        "seo_terms": "tjtest",
                        "create_datetime": "tjtest",
                        "modify_datetime": "tjtest",
                        "enabled": "3",
                        "video_url": "tjtest",
                        "cms_source_id": "3"
                    }

弹性搜索中是否有任何方法我可以指定我只需要返回我的文档,或者如果有人可以建议我如何动态地解析返回的json我欣赏它?

1 个答案:

答案 0 :(得分:1)

只需使用以下内容(其中没有match_all):

client.msearch({
    body: [
        { _index: 'story', _type: 'mytypeee' },
        { query: { query_string: { query: '"this is click tj test"' } } }
    ]
}

关于输出的格式,您可以filter the source甚至使用response filtering option过滤整个回复的所有其他部分。