Thursday, 8 August 2013

Implementation Of Elastic Search



In this article, sharing how to use elastic search for our code. Going to begin by considering examples.
So before going to search anything lets populate some data by using indexing, meaning the "Create" of CRUD, or rather, "indexing". Going to use curl to index data, searching and further operations. Lets start creating indexing of data.
Indexing
Create an index using curl as follows on terminal:
anupam@anupampc:~$ curl -XPUT http://localhost:9200/test-index
{"ok":true,"acknowledged":true}

In order to index a first JSON object you can make a PUT request to the REST API to a URL made up of the index name, type name and ID. That is: http://localhost:9200/<index>/<type>/[<id>].
Post data to index i.e test-index
Ok now insert some data to test-index that I have created earlier.
Run following command:
anupam@anupampc:~$ curl -XPUT localhost:9200/test-index/test-type/1 -d '
{
    "name": "My First Book",
    "author": "Anupam Shakya",
    "pages": 200,
    "book_type": "Novel",
    "price": 100
}'

anupam@anupampc:~$ curl -XPUT localhost:9200/test-index/test-type/2 -d '
{
    "name": "Pinki",
    "author": "A.R. Bosh",
    "pages": 200,
    "book_type": "Comic",
    "price": 200
}'
{"ok":true,"_index":"test-index","_type":"test-type","_id":"2","_version":1}



anupam@anupampc:~$ curl -XPUT localhost:9200/test-index/test-type/3 -d '
{
    "name": "A True Love Story",
    "author": "Shakespeare",
    "pages": 120,
    "book_type": "Fiction",
    "price": 160
}
'
{"ok":true,"_index":"test-index","_type":"test-type","_id":"3","_version":1}

Here in the above example, passing a JSON of book data for example name, author of book, price, number of pages, category of book(book_type) ,
Note: You have to pass a data in a form of JSON with an option -d.

Is it possible to run curl query of indexing data on SENSE tool which is avail for google chrome. Here is an example.
Add JSON to left side column box, put your URL as follows and POST data by pressing Send button.
Provide JSON Data
Result After Posting Data


































Retrieve data that is posted to index(test-index)
It is possible to get data that was posted to the index test-index by the following command of curl
anupam@anupampc:~$ curl -XGET localhost:9200/test-index/test-type/1
Output is as:
{"_index":"test-index","_type":"test-type","_id":"1","_version":1,"exists":true, "_source" :
{
    "name": "My First Book",
    "author": "Anupam Shakya",
    "pages": 200,
    "book_type": "Novel",
    "price": 100
}
 Here is the sample of command on terminal
 It is possible to get the result on SENSE tool. Here is the output.
 Also by putting this URL  - http://localhost:9200/test-index/test-type/1 on browser, you will get the same result.
So, the basic stuff of  elastic search is covered. Lets start with the main stuff that is searching.

Searching
As you already indexed data. Now its time to search.. :)
Syntax for searching data is as <index>/<type>/_search where index and type are both optional.
For example:
a) http://localhost:9200 /test-index/test-type/_search?q=Anupam (Search explicitly for documents of type test-type within the test-index index.)
b) http://localhost:9200 /test-index/_search?q=Anupam (Search in all types of index test-index)
c) http://localhost:9200 /_search?q=Anupam (Search global i.e in all indexes and types)

Let's implement search query using curl
a) When a user want to search explicitly for document in index and in a particular type. As in our case going to serach in index "test-index" and in type "test-type" specifically.
curl -XGET localhost:9200/test-index/test-type/_search?q=Anupam
Output:
{'_shards': {'failed': 0, 'successful': 1, 'total': 1},
 'hits': {'hits': [{'_id': '1',
                    '_index': 'test-index',
                    '_score': 0.59884083,
                    '_source': {'author': 'Anupam Shakya',
                                'book_type': 'Novel',
                                'name': 'My First Book',
                                'pages': 200,
                                'price': 100},
                    '_type': 'test-type'}],
          'max_score': 0.59884083,
          'total': 1},
 'timed_out': True,
 'took': 3}
On SENSE tool the query is as /test-index/test-type/_search?q=Anupam with POST method
 Out put as:
b) Search in all types of index, but specific to an index.
curl -XGET localhost:9200/test-index/_search?q=Anupam
Output:
{'_shards': {'failed': 0, 'successful': 1, 'total': 1},
 'hits': {'hits': [{'_id': '1',
                    '_index': 'test-index',
                    '_score': 0.59884083,
                    '_source': {'author': 'Anupam Shakya',
                                'book_type': 'Novel',
                                'name': 'My First Book',
                                'pages': 200,
                                'price': 100},
                    '_type': 'test-type'}],
          'max_score': 0.59884083,
          'total': 1},
 'timed_out': False,
 'took': 3}
Using SENSE tool. The query as:
Output is as:

Reactions:

0 comments:

Post a Comment