elasticsearch 6.0 request body based searching

Elasticsearch 6.0 request body based searching example using curl with simple steps.


In our example we will learn how can we search a document through request body in Elasticsearch.

cURL is a command line tool that can be used to transfer data over HTTP.In our example we will execute cURL command from cygwin.If searching is complex and wants to use more advanced based searching then body based searching is better.In our example we will use body based searching.

For searching ,i am first indexing more data so that searching can be understood properly

I am indexing following document.

  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/5?pretty' -H 'Content-Type: application/json' -d '{ "name" : "sam", "profession" : "engineer"}'
  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/6?pretty' -H 'Content-Type: application/json' -d '{ "name" : "amit", "profession" : "doctor"}'
  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/7?pretty' -H 'Content-Type: application/json' -d '{ "name" : "joe", "profession" : "teacher"}'
  • Explain

    syntax of cURL command is

    http://localhost:9200/index-name/type-name/document-name

    As we know in Elasticsearch every index have type and every type have document.In our example preparationforinterview is index name and elasticsearch is a type name while 5,6,7 is document name. For REST API Elasticsearch uses 9200 port as default port.

    Elasticsearch automatically adds index , type and mapping in that type.

    Elasticsearch automatically detects that name and profession is text and if new documents is added with more fields then Elasticsearch finds their type and adds it into mapping

  • After indexing document we will execute following command to check all documents.

  • curl "localhost:9200/preparationforinterview/elasticsearch/_search?pretty"



  • To display specific fields in all document under a index and type we can execute below command. As per our searching result above we have two fields in every document but if we want to display only single field for example name then in _source field we can mention only that field. By default _source displays all fields.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "match_all": {}
    },
    "_source": ["name"]
    }'
    


    Note 1 :

  • We can also use wildcards in _source component.For example if we want to search all document with field staring with na
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "match_all": {}
    },
    "_source": ["na*"]
    }'
    


    Note 2 :

  • We can also sort documents based on fields.By default documents are sorted based on _score value in descending order but we can customize sorting based on fields as well. For example if we want to sort the result based on name field in ascending order and if name field clashes then we want to sort based on profession in descending order.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "match_all": {}
    },
    "sort": [
    {"name": "asc"},
    {"profession": "desc"}
    ]
    }'
    

    After executing above command possible that we get following exception because by deafult text fields can not be sorted until and unless we make those field 's fielddata=true



  • To enable those fields for sorting we need to execute following command first before sorting.
  • curl -XPUT 'localhost:9200/preparationforinterview/_mapping/elasticsearch?pretty' -H 'Content-Type: application/json' -d '{
      "properties": {
        "name": {
          "type":     "text",
          "fielddata": true
        },
       "profession": {
          "type":     "text",
          "fielddata": true
        }
      }
    }'
    


  • After making fields enable for sorting , we will execute above sort command again.we get following output.





Visit Others Links

Elasticsearch Introduction
Elasticsearch 6.0 Setup
Elasticsearch Indexing And Searching
Elasticsearch 6.0 indexing example using curl
Elasticsearch 6.0 searching example using curl
Elasticsearch 6.0 updating example using curl
Elasticsearch 6.0 updating example using curl when document does not exist
Elasticsearch 6.0 deleting example using curl
Elasticsearch 6.0 request body based searching
Elasticsearch 6.0 query component example
Elasticsearch 6.0 Analyze API example
Top Elasticsearch Interview Question
© 2019 PreparationForInterview.com