coderquill's inklings

So, How to ... in Elasticsearch?

woman-sitting-office-desk-holding-pen-while-thinking_10045-492


Table Of Contents:


So, you have been using Elasticsearch for some time now and feel like an elastic-ninja…..

……only to not recall command for how to index a document or add a new field ( not me, of course! )


Don't worry, I got you covered! Here is -


How to ….. in Elasticsearch?

Elasticsearch version: 7.6

1. Create an index specifying its settings and mappings:

curl -X PUT "localhost:9200/index_name?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "analyzer_name": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "filter_name"
          ]
        }
      },
      "filter": {
        "filter_name": {
          "type": "stop"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "field_name": {
        "type": "text",
        "analyzer": "analyzer_name"
      }
    }
  }
}
'
    

2. Add a document to an existing index :

Here, a document which we are indexing will have its _id field set as docId.

curl -X PUT "localhost:9200/index_name/_doc/docId?pretty" -H 'Content-Type: application/json' -d'
{
   "field_name":"field_value"
}
'

3. BulkIndex documents from a JSON file to an existing index :

Have a .json file with all the documents to be indexed? Use this!

curl -X GET "localhost:9200/index_name/_mapping?pretty"

4. Add a new field mapping to an existing index:

curl - X PUT "localhost:9200/index_name/_mapping?pretty" - H 'Content-Type: application/json' - d ' {
 "properties": {
  "new_field": {
   "type": "text"
  }
 }
}
'

5. Dump data from one index to another:

Do you want to create a copy index of an existing one? You can use a clone API, but that comes with some prerequisites. Use this simple command to accomplish the same!

elasticdump --input=http://localhost:9200/from_index_name --output=http://localhost:9200/to_index_name

*If you don't create an index and specify settings and mappings, to which data is being added, elastic search creates mappings dynamically*

6. Get mappings for all fields in the index:

curl -X GET "localhost:9200/index_name/_mapping?pretty"

7. Get mappings for a specific field in the index:

curl -X GET "localhost:9200/index_name/_mapping/field/field_name?pretty"

8. Get settings for the index:

curl -X GET "localhost:9200/index_name/_mappings?pretty"

9. Update a field value using update by query:

Here, we are updating a field named fields_name, using a query. Setting its new value as field_value, in all documents.

curl -X POST "localhost:9200/index_name/_update_by_query?conflicts=proceed&pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  },
  "script": "ctx._source.field_name = field_value"
}
'

10. Delete an index:

curl -X DELETE "localhost:9200/index_name?pretty"

11. Delete the JSON document docId from the index 'index_name' :

curl -X DELETE "localhost:9200/index_name/_doc/docId?pretty"

12. Lastly, want to know how an analyzer is processing the data?

Analyze API to the rescue!

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "analyzer" : "standard",
  "text" : "I like programming!"
}
'

which produces the following result:

{ 
  "tokens" : [ 
    { 
      "token" : "i", 
      "start_offset" : 0, 
      "end_offset" : 1, 
      "type" : "<ALPHANUM>", 
      "position" : 0 
    }, 
    { 
      "token" : "like", 
      "start_offset" : 2, 
      "end_offset" : 6, 
      "type" : "<ALPHANUM>", 
      "position" : 1 
    }, 
    { 
      "token" : "programming", 
      "start_offset" : 7, 
      "end_offset" : 18, 
      "type" : "<ALPHANUM>", 
      "position" : 2 
    } 
  ] 
}

i.e. after analyzing the given string by a simple analyzer, it produces tokens as "i", "like", and "programming".

So, by using this API, we can understand what a particular analyzer is exactly doing!

💫

1

  1. If you found this helpful, please share it to help others find it! Feel free to connect with me on any of these platforms=> Email | LinkedIn | Resume | Github | Twitter | Instagram 💜