Indexing and Searching Documents with Elastic - Part 2

Indexing and Searching Document with Elastic

In the previous part we learned some basics about elastic and how to get use of the kopf plugin. With this plugin you can call the rest endpoints just with your browser. You can also use the kopf plugin, or curl to run the following rest request to elastic.

 

Indexes and Types

A document is written into an index. In the previous part we learned what an index is and how indexes get distributed as shards.



An index has a second dimension called "type". With this type you can split documents logically that belong to the same index.

 

The url for a docuement is allways according to the following scheme:

http://elaticnode:elasticport/index/type/id

Writting documents with curl

If an index does not exist, it will be created automatically when you push the first document. By default an elastic index is schemaless and the datatype of a field is determined automatically. This makes it simple to just push a JSON document to the api:

 

curl -PUT -d '{"name": "hello world"}' http://localhost:9200/my_first_index/my_first_type

In the request an id of the document is completly missing. This id is generated automatically on the fly by elastic. The response is showing if the document has been index succesfull or not.

 

{
    "_index":"my_first_index",
    "_type":"my_first_type",
    "_id":"AVD_uiMUngvjD4G5EJK9",
    "_version":1,
    "_shards":
       {"total":2,"successful":1,"failed":0},
    "created":true
}

With a GET request you can retrieve the document, that we pushed in the previous step:

 

curl -GET http://localhost:9200/my_first_index/my_first_type/AVD_uiMUngvjD4G5EJK9

{
   "_index":"my_first_index",
   "_type":"my_first_type",
   "_id":"AVD_uiMUngvjD4G5EJK9",
   "_version":1,
   "found":true, 
   "_source":{"name": "hello world"}
}

Now we create an additional document to be able to play a little bit with the elastic query syntax:

 

curl -PUT -d '{"name": "hello plante"}' http://localhost:9200/my_first_index/my_first_type

By now we've indexed two document into elastic. The first document has the name "hello world" the second one has the name "hello planet".

 

Writting a simple Search Query for Elastic

Spätestens bei solchen Queries ist das Handling mit Curl unpraktisch ;) und ein Curl Plugin oder Kopf leisten gute Dienste.

 

With nested queries like this, the handling with curl is getting complucated and a curl browser plugin or the elastic kopf plugin is very helpful.

 

GET http://localhost:9200/my_first_index/my_first_type/_search
{
  "query": {
    "match": {
      "name": "hello"
    }
  }
}

In the response we can see both documents:

 

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.19178301,
    "hits": [
      {
        "_index": "my_first_index",
        "_type": "my_first_type",
        "_id": "AVD_yeetngvjD4G5EJLD",
        "_score": 0.19178301,
        "_source": {
          "name": "hello planet"
        }
      },
      {
        "_index": "my_first_index",
        "_type": "my_first_type",
        "_id": "AVD_uiMUngvjD4G5EJK9",
        "_score": 0.19178301,
        "_source": {
          "name": "hello world"
        }
      }
    ]
  }
}

Navigation