Indexing/Replacing Documents in Elasticsearch

I, Arslan Mirbzergi,in this article, I’m going to familiarize you with how to manage data in Elasticsearch. In the previous part, we talked about making indexes.
If you haven’t read the first part, I suggest you read the first part:

Part 1

First example:

Now we want to teach you to add a Documents document to your previous index which is already created with the name Customer. For this purpose, we first use the request below.

elasticsearch

elasticsearchThis command creates a document with id=1 and stores it in an index called customer. If we repeat this command with the same document, Elasticsearch will create a new document with id=1 on the previous document.

This time we’ll try a new command.

elasticsearch3

elasticsearch4

As you can see, after getting a new document, the name field value has changed. In Elasticsearch, it is not necessary to set a value for id, and if you do not need to add id to create a specific document, you can use the POST command. You can see the example of this command below.

elasticsearch5

As you can see in the -id field, in this case a random id for document is determined by Elasticsearch.

Update Documents in Elasticsearch

Elasticsearch, in addition to indexing and replacing documents, also has the ability to update a document. To do this, it first erases the previous document and then indexes the updated document. In the example below, you will see how to update a document.

elasticsearch6

As you can see, after getting the document, the name field value has been changed correctly to NilaSoftware.

elasticsearch7

In the next example, we also add the age field to our document.

elasticsearch8

Simple scripts are sometimes used in the update process. For example, we first increase the age field value of the document to 10 using script. In the example below, the phrase ”ctx._source” refers to the document itself and refers ctx._source.age” to the age field of that document.

elasticsearch9

After getting the document, we see that the age value has increased to 10.

elasticsearch10

Clear Documents in Elasticsearch

To erase document with id=2, we use the following api.

elasticsearch10

Batch Processingin Elasticsearch

Another feature of Elasticsearch is the ability to index, update, and erase documents in batches. This increases the speed of operations compared to the mode in which operations are performed individually on each document. Below are 2 simple examples of this feature.

elasticsearch12

13elasticsearch

We were familiar with basic concepts such as Cluster, index and big data about Elasticsearch. I am Arslan Mirbzergi,in this article I’m going to familiarize you with how to search among data in Elasticsearch. Let’s assume we have a document with the following design.

elasticsearch14

Loading sample data in Elasticsearch

First, download a data and index it in your cluster via the following command.

curl -H “Content-Type: application/json” -XPOST

“localhost:9200/bank/_doc/_bulk?pretty&refresh” –data-binary “@accounts.json”

After that, get your indexes.

curl “localhost:9200/_cat/indices?v”

This will get a response as follows.

elasticsearch15

The Search Api in Elasticsearch

In Elasticsearch, there are two ways to search for data. One way to do the head is through Rest Request URl and the other is through Rest Request Body. Thearchs that use request body are clearer, and this header is a more legible format in JSON.

In this article, we review the header via Rest Request Body, but to help you understand thesearchs, we’ll take an example about The Head with Rest Request URL together.

To do this type of conduct, we use an api with endpoint equal to _search.

elasticsearch17

In Api, the parameter q=*indicates that Elasticsearch loads all documents.

elasticsearch18

The above expression indicates that all existing documents are arranged ascendingly using the account_number field. Below are some examples of the fields used in elasticsearch together.

took :

This indicates that the stretch time by elasticsearch is in milliseconds.

timed_out :

Time shows whether or not the head is out.

shards_ :

It shows the number of shards on which the head was carried out, as well as the number of successful and unsuccessful shards.

hits :

Displays the Sarch response.

hits.total :

Indicates the number of documents that are consistent with ourarch parameters.

hits.hits :

Displays real arrays (real data).

hits.sort:

The key is sorting.

Now we want to do a header operation like the previous example and this time via Rest Request Body together.

elasticsearch19

Introduction to query language in Elasticsearch:

Example:

Here is the field to search all documents in a specific index.

elasticsearch20

In the next section, we add the size expression and place its value equal to 1. This will allow only one document to be returned in response.

elasticsearch21

The size value by default is always equal to 10. In this section, the phrase from means determining which document to start from and how many documents to return according to the size value. The value of the phrase from by default is equal to zero.

elasticsearch22

elasticsearch22

In the request above, all documents are returned descendingly by the balance field. Up to this point, you are familiar with a few basic parameters and you can do more advanced searches from now on. In the example below, you can get the request below with two balance fields and account_number.

elasticsearch24

request after running fields:

elasticsearch25

In this section, we are going to use a new query called match.

Supposing we want to find a document with account_number equal to 20. For this purpose, we use the following commands.

elasticsearch

Response of these commands will be as follows.

As you can see, only one document with account_number is equal to 20.

Second example:

Let’s take another example to better understand this together. This time we use the example of the following code. In this section, all documents with mill and lane values are displayed in the address field. In order to determine which documents exactly contain mill lane to be displayed, we need to use a query called match_phrase.

There is another query called bool. The task of this query is to display all the smaller query in a larger query. This is done by a logical means called boolean. In the example below, with two times the use of match query, all documents containing mill and lane values are displayed in the address field.

Response of these commands will be as follows.

Note the highlighted address field here. This field contains both mill and lane values. The phrase must in the request means that getting the true value is mandatory for both bool query in order to display the document.

In the request above, you’ll see a new query called should. This query returns the document when it is used, if only one of the bools has a true value.

In the above code sample, you will see another new query called must_not. This query does not return any documents containing mill and lane values in the address field when used.

Now let’s look at another example, in this instance, we want to show documents that have age values equal to 40 and whose state is not equal to ID. For this purpose, we use the following code.

Apply filters in Elasticsearch

You remember that in the Search Api section, we skiped document score. In this section, we want to give you a brief description of the score in Elasticsearch. Score in Elasticsearch is a numeric value and is valued based on document and query coordination in request. In some cases, it is practically useless to rate a score. For example, the Elasticsearch system for documents does not generate a specific score value because this score value will not actually be used in filtering a specific field in Elasticsearch. In the bool query we introduced in the previous section, filter conditions are used to apply filters on documents.

Last example:

Here’s another example to conclude the discussion. In this example, by query range, by range, we can filter documents based on different values of a field. Range query is used to filter numeric values or dates.

In the example below, accounts with balance values between 20,000 and 50,000 are displayed.