Friday, 9 August 2013

Apache Issue When Restart



Solution for: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName


Sometimes while restarting the apache server, using this command
 sudo /etc/init.d/apache2 restart

Get output as:
* Restarting web server apache2                                                                                                   apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
                                                                                                                            [ OK ]


This can be fixed by providing the server name in the httpd.conf file of apache2

sudo gedit /etc/apache2/httpd.conf

By default its an empty file, put this content into this file

ServerName localhost

Save this file and now restart apache.

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:

Tuesday, 6 August 2013

ElasticSearch Installation

Installation Of Elastic Search
Elastic search is real time distributed search engine. Its very fabulous to use elastic search for projects instead of creating own search strategies
So here are the steps to install elastic search:
  • Download debian package of elastic search from official site.
  • Install JDk by - sudo apt-get install openjdk-6-jdk
  • Install elsatic search - sudo dpkg -i elasticsearch-0.90.2.deb (path of debian package of elasticsearch) 
How you can test whether elastic search is installed in your system. It is possible to test by putting this URL on browser http://localhost:9200/ , by default elastic search working on the port of 9200 and return following json on browser.

{
  • ok: true,
  • status: 200,
  • name: "Lodestone",
  • version: {
    • number: "0.90.2",
    • snapshot_build: false,
    • lucene_version: "4.3.1"
    },
  • tagline: "You Know, for Search"
}

Number of options are avail for elastic search. Run following command on terminal to see options.
a) anupam@anupampc:~$ sudo service elasticsearch help
Output 
 * Usage: /etc/init.d/elasticsearch {start|stop|restart|force-reload|status}


b) anupam@anupampc:~$ sudo service elasticsearch start
Output
* Starting ElasticSearch Server                                        [ OK ]

c) anupam@anupampc:~$ sudo service elasticsearch status
Output

* ElasticSearch Server is running with pid 4996
 

d) anupam@anupampc:~$ sudo service elasticsearch restart
Output

* Stopping ElasticSearch Server                                        [ OK ]

Plugin avail for using elastic search in chrome
SENSE plug-in avail for google chrome. Add sense to your favorite google chrome and ready to use elasticsearch. Once you have installed it you'll find Sense's icon in the upper right corner in Chrome. The first time you click it and run Sense a very simple sample request is prepared for you. It is possible to run queries directly on sense and see results. 

Sense Interface On Google Chrome


Sense provides a simple user interface specifically for using ElasticSearch's REST API. It also has a number of convenient features such as autocomplete for ElasticSearch's query syntax and copying and pasting requests in curl format, making it easy to run examples from the documentation.