4

Elastic Search Code :

POST /_msearch
{ "index": "INDEX_NAME_1", "type": "TYPE_NAME_1" }
{ "query": { "match_all": {}}}
{ "index": "INDEX_NAME_2", "type": "TYPE_NAME_2" }
{ "query": { "match_all": {}}}

Reference link http://teknosrc.com/execute-multiple-search-query-elasticsearch/#comment-8578 (Refer example 1)

Node js code :

return new Promise(function (resolve, reject) {
        elasticClient.search({
             index: '*:logstash-prod-*',
             type: 'callEnd',
             size: 10000,
             body: {
                 query: {

                     range: {
                         "@timestamp": {
                             "gte": startTime,
                             "lte": endTime
                         }
                     }
                 }
             },
         }, function (error, response, status) {
             if (error) {
                    reject(error);
             }
             else {
                     console.log("show the response" + JSON.stringify(response));
                     let elasticResponse=response.hits.hits;
                     resolve(response);
             }
         })
     });

Above node js query works for one type:"callEnd". Please help in converting the Elastic code (two types) to node js code.

1 Answer 1

8

Here is msearch documentation.

In your case it'll be something like this:

const queryBody = [
    { index: '*:logstash-prod-*', type: 'callEnd1' },
    {
        query: {
            range: {
                '@timestamp': {
                    'gte': startTime,
                    'lte': endTime
                }
            }
        },
        size: 10000
    },
    { index: '*:logstash-prod-*', type: 'callEnd2' },
    {
        query: {
            range: {
                '@timestamp': {
                    'gte': startTime,
                    'lte': endTime
                }
            }
        },
        size: 10000
    }
];

return elasticClient
    .msearch({ body: queryBody })
    .then(result => {
        console.log('show the response' + JSON.stringify(result));

        return result;
    })
    .catch(error => {
        // TODO Handle error here.
    });

Note that msearch returns promise itself, so no need to create it yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.