RamRajVasavi
RamRajVasavi

Reputation: 906

get documents based on property values using elasticsearch in nodejs

I am using Elasticsearch and i am new to this technology.I didnot know how to search property values on indices types in elasticsearch using nodejs.My data is

{
           "line_num": 1,
           "action1": "ORIGINAL",
           "process_code1": "PENDING",
           "isPR": "Y",
           "req_header_reference_num": "PC5298",
           "req_line_reference_num": "PC5298",
           "pr_line_no": "1",
           "pr_id": "ffd3f7cc6",
           "pr_po_flag": "N",
           "pr_item_id": "720541"
 }

from this data i want to search based on property value. Example If i give "ORIGINAL" ,based on this to get particular document from elasticsearch Then how to solve this problem Thank in advance

Upvotes: 2

Views: 2722

Answers (1)

SylvainB
SylvainB

Reputation: 414

If your goal is to match any field containing the value "ORIGINAL" you must use a "multi match" query.

In your example, this mean :

POST /index/type/_search
{
   "query": {
      "multi_match": {
         "query": "ORIGINAL",
         "fields": [ "*" ],
         "lenient": true
      }
   }
}

But be aware that this way of querying is not very efficient, it is better to match a single field with a "match" query :

POST /index/type/_search
{
   "query": {
      "match": {
          "action1" : "ORIGINAL"
      }
   }
}

In node.js you can use the official elastic library to query elasticsearch more easily.

For example, in your case, you will write this kind of code with this library :

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});
client.search({
    index: 'index',
    type: 'type',
    body: {
        query: {
            match: {
                action1: 'ORIGINAL'
            }
        }
    }
}).then(function (resp) {
    var hits = resp.hits.hits;
    console.log(hits);
}, function (err) {
    console.trace(err.message);
});

Upvotes: 4

Related Questions