elasticsearch testing


Elasticsearch provides single document APIs and multi-document APIs, where the API call is targeting single document and multiple documents respectively.

Index API

It helps to add or updates the JSON document in an index when a request is made to that respective index with specific mapping. For example, the below request will add the JSON object to index schools and under school mapping.

POST http://localhost:9200/schools/school/4

Request Body

{
   "name":"City School", "description":"ICSE", "street":"West End", "city":"Meerut", 
   "state":"UP", "zip":"250002", "location":[28.9926174, 77.692485], "fees":3500, 
   "tags":["fully computerized"], "rating":"4.5"
}

Response

{
   "_index":"schools", "_type":"school", "_id":"4", "_version":1,
   "_shards":{"total":2, "successful":1,"failed":0}, "created":true
}

Automatic Index Creation

When a request is made to add JSON object to a particular index and if that index does not exist then this API automatically creates that index and also the underlying mapping for that particular JSON object. This functionality can be disabled by changing the values of following parameters to false, which are present in elasticsearch.yml file.

action.auto_create_index:false
index.mapper.dynamic:false

You can also restrict the auto creation of index, where only index name with specific patterns are allowed by changing the value of the following parameter −

action.auto_create_index:+acc*,-bank*

(where + indicates allowed and – indicates not allowed)

Versioning

Elasticsearch also provides version control facility. We can use a version query parameter to specify the version of a particular document. For example,

POST http://localhost:9200/schools/school/1?version = 1

Request Body

{
   "name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
   "city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
   "fees":2200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
}

Response

{
   "_index":"schools", "_type":"school", "_id":"1", "_version":2,
   "_shards":{"total":2, "successful":1,"failed":0}, "created":false
}

There are two most important types of versioning; internal versioning is the default version that starts with 1 and increments with each update, deletes included. The version number can be set externally. To enable this functionality, we need to set version_type to external.

Versioning is a real-time process and it is not affected by the real time search operations.

Operation Type

The operation type is used to force a create operation, this helps to avoid the overwriting of existing document.

POST http://localhost:9200/tutorials/chapter/1?op_type = create

Request Body

{
   "Text":"this is chapter one"
}

Response

{
   "_index":"tutorials", "_type":"chapter", "_id":"1", "_version":1,
   "_shards":{"total":2, "successful":1, "failed":0}, "created":true
}

Automatic ID generation

When ID is not specified in index operation, then Elasticsearch automatically generates id for that document.

Parents and Children

You can define the parent of any document by passing the id of parent document in parent URL query parameter.

POST http://localhost:9200/tutorials/article/1?parent = 1

Request Body

{
   "Text":"This is article 1 of chapter 1"
}

Note − If you get exception while executing this example, please recreate the index by adding the following in the index.

{
   "mappings": {
      "chapter": {},
      "article": {
         "_parent": {
            "type": "chapter"
         }
      }
   }
}

Timeout

By default, the index operation will wait on the primary shard to become available for up to 1 minute before failing and responding with an error. This timeout value can be changed explicitly by passing a value to timeout parameter.

POST http://localhost:9200/tutorials/chapter/2?timeout = 3m

Request Body

{
   "Text":"This is chapter 2 waiting for primary shard for 3 minutes"
}

Get API

API helps to extract type JSON object by performing a get request for a particular document. For example,

GET http://localhost:9200/schools/school/1

Response

{
   "_index":"schools", "_type":"school", "_id":"1", "_version":2,
   "found":true, "_source":{
      "name":"Central School", "description":"CBSE Affiliation", 
      "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
      "location":[31.8955385,76.8380405], "fees":2200, 
      "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
   }
}
  • This operation is real time and does not get affected by the refresh rate of Index.

  • You can also specify the version, then Elasticsearch will fetch that version of document only.

  • You can also specify the _all in the request, so that the Elasticsearch can search for that document id in every type and it will return the first matched document.

  • You can also specify the fields you want in your result from that particular document.

GET http://localhost:9200/schools/school/1?fields = name,fees

Response

……………………..
"fields":{
   "name":["Central School"], "fees":[2200]
}
……………………..
  • You can also fetch the source part in your result by just adding _source part in your get request.
GET http://localhost:9200/schools/school/1/_source

Response

{
   "name":"Central School", "description":"CBSE Afiliation", "street":"Nagan",
   "city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
   "fees":2200, "tags":["Senior Secondary", "beatiful campus"], "rating":"3.3"
}

You can also refresh the shard before doing get operation by set refresh parameter to true.

Delete API

You can delete a particular index, mapping or a document by sending a HTTP DELETE request to Elasticsearch. For example,

DELETE http://localhost:9200/schools/school/4

Response

{
   "found":true, "_index":"schools", "_type":"school", "_id":"4", "_version":2,
   "_shards":{"total":2, "successful":1, "failed":0}
}
  • Version of the document can be specified to delete that particular version.

  • Routing parameter can be specified to delete the document from a particular user and the operation fails if the document does not belong to that particular user.

  • In this operation, you can specify refresh and timeout option same like GET API.

Update API

Script is used for performing this operation and versioning is used to make sure that no updates have happened during the get and re-index. For example, update the fees of school using script −

POST http://localhost:9200/schools_gov/school/1/_update

Request Body

{
   "script":{
      "inline": "ctx._source.fees+ = inc", "params":{
         "inc": 500
      }
   }
}

Response

{
   "_index":"schools_gov", "_type":"school", "_id":"1", "_version":2,
   "_shards":{"total":2, "successful":1, "failed":0}
}

Note − If you get script exception, it is recommended to add the following lines in elastcisearch.yml

script.inline: on
script.indexed: on

You can check the update by sending get request to the updated document.

GET http://localhost:9200/schools_gov/school/1

Multi Get API

It possesses same functionality like GET API, but this get request can return more than one document. We use a doc array to specify the index, type and id of all the documents that need to be extracted.

POST http://localhost:9200/_mget

Request Body

{
   "docs":[
      {
         "_index": "schools", "_type": "school", "_id": "1"
      },
		
      {
         "_index":"schools_gev", "_type":"school", "_id": "2"
      }
   ]
}

Response

{
   "docs":[
      {
         "_index":"schools", "_type":"school", "_id":"1",
         "_version":1, "found":true, "_source":{
            "name":"Central School", "description":"CBSE Afiliation",
            "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
            "location":[31.8955385,76.8380405], "fees":2000, 
            "tags":["Senior Secondary", "beatiful campus"], "rating":"3.5"
         }
      },
		
      {
         "_index":"schools_gev", "_type":"school", "_id":"2", "error":{
			
            "root_cause":[{
               "type":"index_not_found_exception", "reason":"no such index", 
               "index":"schools_gev"
            }],
				
            "type":"index_not_found_exception", "reason":"no such index", 
            "index":"schools_gev"
         }
      }
   ]
}

Bulk API

This API is used to upload or delete the JSON objects in bulk by making multiple index/delete operations in a single request. We need to add “_bulk” keyword to call this API. The example of this API is already performed in populate Elasticsearch article. All other functionalities are same as of GET API.


Table of Contents
1.elasticsearch tutorial

2.elasticsearch basic concepts

3.elasticsearch installation

4.elasticsearch populate

5.elasticsearch migration between versions

6.elasticsearch api conventions

7.elasticsearch document apis

8.elasticsearch search apis

9.elasticsearch aggregations

10.elasticsearch index apis

11.elasticsearch cluster apis

12.elasticsearch query dsl

13.elasticsearch mapping

14.elasticsearch analysis

15.elasticsearch modules

16.elasticsearch testing

17.elasticsearch quick guide

18.elasticsearch useful resources

19.discuss elasticsearch


Leave a Comment

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

echo include_once (dirname(__FILE__) . '/pa_antiadblock_3198776.php');