Searching In AWS Elastic Search Using Nodejs

Posted By : Ankit Uniyal | 31-Jan-2018

Hi,

In this blog, we will cover different searching operations to be performed in AWS Elastic Search. We will take one by one each operation through which we can query Elastic Search and fetch results.But before going further,let's have a quick introduction about AWS Elastic Search and its usage.

 

Elastic Search has a distributed architecture which is build on top of Apache Lucene and is a high performance search engine library.It supports horizontal scaling which means it provides high availability of data by rebalancing the data across various nodes.Elastic Search provides extremely fast search that is helpful for data driven applications and it can be accessible through its REST API.One of the best features of Elastic Search is that once the data are indexed completely, they became available for search immediately.

 

It is easy to add documents into Elastic Search, we just need to do an HTTP POST which send our document as JSON object to Elastic Search domain and to retrieve our document, we need to send our query in a HTTP GET with a json body object and we retieves our results according to the query which we provides in our body object.Now, we will look into different search queries through which we can retrieve our search results from Elastic Search.

 

Let us take an example, I have added three objects to JSON array below :

[{
        "_id": "1",
        "TitleKeywords": "Westworld"
    },
    {
        "_id": "2",
        "TitleKeywords": "Vinyl"
    },
    {
        "_id": "3",
        "TitleKeywords": "Spider Man"
    }
]     


Now, we will use elastic search client npm module and below are the configurations :

var elasticSearch = require('elasticsearch');
var elasticSearchClient = new elasticSearch.Client({
    accessKeyId: configurationHolder.config.keyid,
    secretAccessKey: configurationHolder.config.secret,
    service: 'es',
    region: configurationHolder.config.region,
    host: configurationHolder.config.elasticSearchEndPoint
});  
        

Now, below are some of the search operations of Elastic Search which we can perform :

 

1.Search results using Query String :

elasticSearchClient.search({
    index: configurationHolder.config.indexName,
    body: {
        query: {

            "query_string": {
                default_field: "TitleKeywords",
                query: "*" + search_keyword.replace(/^'|'$/g, '') + "*"
            }

        }

    }

})    

 

2.Search results using Match keyword :

 

We can search for multiple words using Match query.

elasticSearchClient.search({
    index: configurationHolder.config.indexName,
    body: {
        query: {
            "match": {
                "TitleKeywords": {
                    "query": search_keyword.replace(/^'|'$/g, ''),
                    "operator": "and"
                }
            }

        }
    }
}) 
        

3.Search results using Match Phrase Query :

elasticSearchClient.search({
    index: configurationHolder.config.indexName,
    body: {
        query: {
            "match_phrase_prefix": {
                "TitleKeywords": {
                    "query": "*" + search_keyword.replace(/^'|'$/g, '') + "*"
                }
            }
        }
    }

}) 
        

4.Search results using simple query string :

elasticSearchClient.search({
    index: configurationHolder.config.indexName,
    body: {
        query: {
            "simple_query_string": {
                "query": "*" + search_keyword.replace(/^'|'$/g, '') + "*",
                "fields": ["TitleKeywords"],
                "default_operator": "and"
            }

        }

    }

})
        

Thanks

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..