设为首页 收藏本站
云服务器等爆品抢先购,低至4.2元/月
查看: 1132|回复: 0

[经验分享] ElasticSearch(1)Installation and Simple Use

[复制链接]

尚未签到

发表于 2017-5-20 14:09:19 | 显示全部楼层 |阅读模式
  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/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379400-1-1.html 上篇帖子: Elasticsearch安装中文分词插件ik 下篇帖子: ElasticSearch大批量数据入库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表