Search code examples
elasticsearchelasticsearch-pluginelasticsearch-5

How to filter the returned json search result of elastic search to just my documents


I am using AWS elastic search and the following documents to retrieve my data:

elastic search

and specifically I use msearch as in javascript as follows:

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));
});

}

An I can see all the result as follows:

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

}

But as you it is very difficult to parts and get just the documents needed. I just need this part:

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

Is there any way in elastic search I can specify that I just need to return my documents or if anyone can suggest a good way how I can parse the returned json dynamically I appreciate it?


Solution

  • Just use the following (no match_all in there):

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

    Regarding the format of the output, you could filter the source or even filter all other parts of the whole response using response filtering option.