ElasticSearch Query Samples

Reading Time: 2 minutes

This post is about sample queries for full text search. Elasticsearch query samples using NEST (.NET) and Java High Level REST Client (Java) are provided in master and java branches respectively. Note that query details are documented in ISearchService.cs  and ISearchService.java interfaces.

Query Structure

HTTP VERB /{index}/{type}/{id}

Indexing

Before implementing a full text search, we first need to index documents.

Create Document with Specific ID

PUT verb is used

PUT /company/product/10
{
   "name": "Sony",
   "price": "450",
   "currency": "EUR"
}

Result:

{
   "_index": "company",
   "_type": "product",
   "_id": "10",
   "_version": 1,
   "result": "created",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": true
}

Create Document with Automatic ID Generation

Note that POST used instead of PUT.

POST /company/product/
{
   "name": "Sony",
   "price": "450",
   "currency": "EUR"
}

Result:

{
   "_index": "company",
   "_type": "product",
   "_id": "AV6Aprv0Xg5KtQ3ImYfh",
   "_version": 1,
   "result": "created",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": true
}

Query Samples

Queries that I implemented are explained in this section.

Searching all products

GET /company/product/_search/

Using query DSL for more usefull search

In order to make a more useful search request, we also need to supply a request body with a query.

POST /company/product/_search/
{
    "query": {
        "query_string": {
          "query": "Huawei",
        }
      }
}

Fine tuning query string queries

Retrieve documents with the name of “Huawei”

POST /company/product/_search/
{
    "query": {
        "query_string": {
          "query": "Huawei",
          "fields": [
            "name"
         ]
        }
      }
}

Searching for multiple products

Retrieve documents with the name of “Huawei and iPhone6s”

POST /company/product/_search/
{
    "query": {
        "query_string": {
          "query": "Huawei iPhone6s",
          }
      }
}

Filtering

Filters the products whose name is “Samsung” and price is between gte (e.g.: 1000) and lte (e.g.: 4000).

POST /company/product/_search/
{
  "query": {
      "bool": {
	"must": {
	    "match": {
		"name": "Samsung"
             }
	},
	"filter": [
	   {
	    "range": {
		"price": {
		   "gte": 1000,
		   "lte": 4000
	         }
	     }
	   }
	]
     }
  }
}

Filters the products whose name is “Samsung” and price is 2900

POST /company/product/_search/
{
   "query": {
      "bool": {
         "must": {
            "match": {
               "name": "Samsung"
            }
         },
         "filter": {
            "term": {
               "price": 2900
            }
         }
      }
   }
}

Sorting

Sort all products in ascending order.

POST /company/product/_search/
{
   "query": {
     "match_all": {}
   },

   "sort": {
      "price": "asc"

   }
}

Sort products with a name “Samsung” in ascending order.

POST /company/product/_search/
{
   "query": {
      "query_string": {
         "query": "Samsung"
      }
   },
   "sort": {
      "price": "asc"
   }
}

Pagination

Elasticsearch returns the first ten hits by default. You can change it via size parameter. Following query retrieves the first 20 queries.

POST /company/product/_search/
{
   "query": {
     "match_all": {}
   },
   "size": 12
}

Retrieving only parts of documents

Add  _source property to the query. Following query retrieves only “name” of all products. It is also possible to retrieve multiple fields as well using “_source”: [“property1”, “property2”]

POST /company/product/_search/
{
  "_source": "name"
}

Result:

{
	"_index": "company",
        "_type": "product",
	"_id": "AV57gFjUQ8Qv2Iw4gRgt",
	"_score": 1,
	"_source": {
		"name": "iPhone7"
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *