elasticsearch 6.0 searching example using curl

Elasticsearch searching example using curl with simple steps.


In our example we will learn how can we search a document using curl 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.So to execute cURL command from cygwin first go to folder that have curl.exe.

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 search all document.

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



  • To search specific field using Elasticsearch, we can execute following command.For example in below command we are searching all document which has word sam and wants to display only name and profession field
  • curl "localhost:9200/preparationforinterview/elasticsearch/_search?q=sam&_source=name,profession&pretty"



    Note :

  • To search in multiple type
  • curl "localhost:9200/preparationforinterview/elasticsearch,elasticsearch2/_search?q=sam&_source=name,profession&pretty"
  • To search in all type
  • curl "localhost:9200/preparationforinterview/_search?q=sam&_source=name,profession&pretty""
  • To search in all indices
  • curl "localhost:9200/_search?q=sam&_source=name,profession&pretty"
  • In above example we are passing search criteria in URI but if search is complex then we give search criteria in JSON format.Syntax for this is
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{

    "query": {

    "query_string": {

    "query": "doctor"

    }

    }

    }'






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