elasticsearch - 如何将 Elasticsearch 返回的json搜索结果过滤到我的文档中

标签 elasticsearch elasticsearch-plugin elasticsearch-5

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

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"
                    }

flex 搜索中有什么方法可以指定我只需要返回我的文档,或者如果有人可以提出一种很好的方法来动态解析返回的json,我将不胜感激?

最佳答案

只需使用以下命令(其中没有match_all):

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

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

关于elasticsearch - 如何将 Elasticsearch 返回的json搜索结果过滤到我的文档中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47045527/

相关文章:

elasticsearch - 如果是多重排序,Elastic Search的响应速度会变慢吗?[不是脚本排序]

elasticsearch - 为什么我的elasticsearch查询缓存是空的?

java - 使用 JestClient 获取 Double 值而不是 Integer 从 elasticsearch 检索文档

python - 在汇总中对_score最好的 Elasticsearch 结果进行排序

elasticsearch - 有效地扫描和查找Elasticsearch索引中的敏感数据

scala - 在scala eclipse插件中构建查询/过滤器以进行 Elasticsearch

elasticsearch - 有没有办法像其他编程语言一样用脚本迭代弹性数组文档

node.js - client.indices.putTemplate 和 client.putTemplate 之间的区别

elasticsearch - 如何为我们在Elastic Search中创建的查询添加搜索选项?

elasticsearch - 将索引从Elasticsearch 1.x迁移到Elasticsearch 2.x的工具