`
sillycat
  • 浏览: 2578956 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ElasticSearch(1)Installation and Simple Use

 
阅读更多

ElasticSearch(1)Installation and Simple Use

1. Introduction and Installation
Elasticsearch is based on Apache lucene. ES provide RESTful API.

Download the last one from here http://www.elasticsearch.org/download/
I download the version 1.4.0 this time. https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.0.tar.gz

Unzip the file and place it in the working directory.

Start the Server
>bin/elasticsearch

Visit this URL
http://localhost:9200/?pretty

{
  "status" : 200,
  "name" : "Mar-Vell",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.0",
    "build_hash" : "bc94bd81298f81c656893ab1ddddd30a99356066",
    "build_timestamp" : "2014-11-05T14:26:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}
We install and start the server successfully.

2. API 
Java API
Node Client: The Node client will join the cluster. The system does not have any data in Node Client. But Node Client know where is the data.
Transport Client: It will not join the cluster, but it will send the request to the cluster.
http://www.elasticsearch.org/guide/

It seems that we have Java, JavaScript, Groovy, PHP, Perl, Python, Ruby API.

HTTP RESTful API
use CURL to check how many files we have
>curl -XGET 'http://localhost:9200/_count?pretty' -d '
> { > "query":{ > "match_all":{} > } > } > ' {  "count" : 0,  "_shards" : {    "total" : 0,    "successful" : 0,    "failed" : 0  } }

-i will ask curl to response the header information
>curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 336
…snip…

3. Document Oriented
JSON, ES(elastic search) will store and index the JSON data.

4. Build Index 
Create a Employee List
Normal Database ——> Database ——> Table ——> Row ——> Columns
Elasticsearch       ———> Index   ——>Type ——>Doc ——> Fields

create the index
>curl -XPUT 'http://localhost:9200/megacorp/employee/1' -d '
> { > "first_name" : "Carl", > "last_name" : "Luo", > "age": 33, > "about": " I love java, python, scala, groovy", > "interests": [ "sports", "music"] > } > ' {"_index":"megacorp","_type":"employee","_id":"1","_version":1,"created":true}

megacorp is the index name, something like database
employee is the type name, something like the table
1 is the id of the doc.

Create some Other Data
>curl -XPUT 'http://localhost:9200/megacorp/employee/1' -d '
> {> "first_name" : "Carl",> "last_name" : "Luo",> "age": 33,> "about": " I love java, python, scala, groovy",> "interests": [ "sports", "music"]> }> '

>curl -XPUT 'http://localhost:9200/megacorp/employee/3' -d '
> { > "first_name" : "Douglas", > "last_name" : "Fir", > "age" : 35, > "about" : "I like to build cabinets", > "interests" : [ "forestry" ] > } > '

5. Search the Doc
Something like key value, if we plan to fetch some data, just do like this.
>curl -XGET 'http://localhost:9200/megacorp/employee/1'
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source": { "first_name" : "Carl", "last_name" : "Luo", "age": 33, "about": " I love java, python, scala, groovy", "interests": [ "sports", "music"] }

GET, DELETE

Something Useful for Search
List all the employee
>curl -XGET 'http://localhost:9200/megacorp/employee/_search'

Query String for Search
>curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith'
{"took":25,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":0.30685282,"_source": { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : " I like to collect rock albums", "interests" : [ "music" ] }

_search?q=last_name:Smith

Using Query DSL
Domain Specific Language, JSON
>curl -XGET 'http://localhost:9200/megacorp/employee/_search' -d '
{ "query" : { "match" : { "last_name" : "Smith" } } }'

Try Complex Query
curl -XGET 'http://localhost:9200/megacorp/employee/_search' -d ' > { > "query" : { > "filtered" : { > "filter" : { > "range": { > "age" : { "gt" : 30 } > } > }, > "query" : { > "match" : { > "last_name" : "Smith" > } > } > } > } > } > '

Add Filter for the age > 30. gt is short for Greater Than.

6. Search the Full Document
{
     “query” : {
          “match” : {
               “about” : “rock climbing"
          }
     }
}

It will return the about column contains “rock”, “climbing”, “rock climbing"

{
     “query” : {
          “match_phrase” : {
               “about” : “rock climbing"
          }
     }
}

match phrase will only return the “rock climbing”

High Light the Search Result
>curl -XGET 'http://localhost:9200/megacorp/employee/_search' -d '
{ > "query":{ > "match_phrase":{ > "about" : "rock albums" > } > }, > "highlight":{ > "fields":{ > "about":{} > } > } > } > ' {"took":26,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":0.23013961,"_source": { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : " I like to collect rock albums", "interests" : [ "music" ] } ,"highlight":{"about":[" I like to collect <em>rock</em> <em>albums</em>"]}}]}}

7. Aggregations
curl -XGET 'http://localhost:9200/megacorp/employee/_search' -d ' {   "aggs":{      "all_interests":{        "terms": { "field" : "interests" }      }   } } '

http://fuxiaopang.gitbooks.io/learnelasticsearch/content/getting_started/tutorial_aggregations.html


References:
http://www.elasticsearch.org/overview/


http://fuxiaopang.gitbooks.io/learnelasticsearch/content/
http://www.oschina.net/translate/elasticsearch-getting-started
http://liuhongjiang.github.io/tech/blog/2013/01/11/es/
http://nkcoder.github.io/blog/20140217/elasticsearch-install-config/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics