ElasticSearch

ElasticSearch

Check your elasticsearch

curl -X GET http://localhost:9200

Indexing

curl -XPUT http://localhost:9200/twitter/tweet/1
{
“user”:”Kimchy”,
“post_data”:”2009-11-15T14:12:12”,
“message”:”Trying out Elastcisearch,so far so good?”,
}

curl -XPUT http://localhost:9200/twitter/tweet/1
{
“user”:”Kimchy”,
“post_data”:”2009-11-15T14:12:12”,
“message”:”Another tweet,will it be indexed?”,

}

Now, let’s see if the information was added by Getting it:

curl -XGET ‘http://localhost:9200/twitter/user/kimchy?pretty=true
curl -XGET ‘http://localhost:9200/twitter/tweet/1?pretty=true
curl -XGET ‘http://localhost:9200/twitter/tweet/2?pretty=true

Searching

To find all the tweets that @kimchy@ posted:

curl -XGET ‘http://localhost:9200/twitter/tweet/_search?q=user:kimchy&pretty=true

we can also use the JSON query language Elasticsearch

curl -XPOST ‘http://localhost:9200/twitter/tweet/_search
{
“query”:{
“match”:{“user”:”kimchy”}
}
}

let’s get all the documents stored

curl -XPOST ‘http://localhost:9200/twitter/tweet/_search
{
“query”:{
“match_all”:{}
}
}

we can also do range search
curl -XPOST ‘http://localhost:9200/twitter/tweet/_search
{
“query”:{
“range”:{
“post_date”:{“form”:”2009-11-15T13:00:00”,”to”:”2009-11-15T14:00:00”}
}
}
}

Muti Tenan-Indices and Types

Elasticsearch supports multiple indices, as well as multiple types per index,In the previous example we used an index called @twitter@ and two types @user@ and @tweet@

Complete control on the index level is allowed, As an example , in the above case,we would want to change from the default 5 shard with 1 replica per index, to only 1 shared with 1 replica per index.

curl -XPUT http://localhost:9200/another_user
{
“index”:{
“number_of_shards”:1,
“number_of_replicas”:1
}
}

we can easily search on more than one index,for example:

curl -XGET ‘http://localhost:9200/kimchy,another_user/_search
{
“query”:{
“match_all”:{}
}
}

or on all the indices:

curl -XGET ‘http://localhost:9200/_search
{
“query”:{
“match_all”:{}
}
}

Searching the part

GET /website/blog/123?_soure=title,text
the request just filtered the filed of date

if you only want the file of _source, you can send the request
GET /website/blog/123/_source

if you just to check the docoment whether it doesn’t exist?
you can use the head request replace of the get request. for example:

curl -i -XHEAD http://localhost:9200/website/blog/123

if the response is 200 ok, then the document is exist. however, if the reponse is 404 NOT Found, the document can be found.

Updating the document

PUT /website/blog/123
{
“tile”:”My first blog entry”,
“text”:”I am starting to get the hang of this…”,
“date”:”2014/01/02”
}

we can see the _version in the response is adding one.

Creating the document that has already existed or does not existed

PUT /website/blog/123/_create
{…}

create new document:the code 201
already existed: the conflict code 409

Delete document

Delete /website/blog/123

found:code 200
not found:code 404

how to deal with conflict

two common way to handle the conflict

Pessimistic concurrency control

Optimistic concurrency control

when we update the document, we should add the filed version paramer in the request.

PUT /website/blog/1?version=1

we use the external version control

PUT /website/blog/2?version=5&version_type=external
{
“title”:“My first external blog entry”,
“text”:”Starting to get the hang of this…”
}

if you want to repeat the request, the version in the external version control request, the number of version should be bigger than the number of version in the elasticsearch.

we update the part of the document

POST /website/blog/1/_update
{
“doc”:{
“tag”:[‘testing’],
“views”:0
}
}

we can see in the above request, we add two fileds tags and views in the document.

we can use the script to update the document

some things that we can refer to the elasticsearch’s offical document

search multi-documents

_mget comes from multi-get

POST /_mget
{
“docs”:[
{
“_index”:”website”,
“_type”:”blog”,
“_id”:2
},
{
“_index”:”website”,
“_type”:”pageviews”,
“_id”:”1”,
“_source”:”views”
}
]
}

this is the above request.
this is the below response.
{
“docs”: [
{
“_index”: “megacorp”,
“_type”: “employee”,
“_id”: “2”,
“_version”: 3,
“found”: true,
“_source”: {
“first_name”: “Jane”,
“last_name”: “Smith”,
“age”: 32,
“about”: “I like to collect rock albums”,
“interests”: [
“music”
]
}
},
{
“_index”: “megacorp”,
“_type”: “employee”,
“_id”: “1”,
“_version”: 3,
“found”: true,
“_source”: {
“first_name”: “John”,
“last_name”: “Smith”,
“age”: 25,
“about”: “I love to go rock climbing”,
“interests”: [
“sports”,
“music”
]
}
}
]
}

if you just want to search document with the same index and different types, you can send the request as below:

POST /website/blog/_mget
{
“docs”:[
{“_id”,2},
{“_type”:”pageviews”,”_id”:1}
]
}

POST /website/blog/_mget
{
“ids”:[“2”,”1”]
}

Batch update operation

_bulk

the request of bulk

{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n

action/metadata this line is used to define what action in which document.

action must be in the blew serveral action

actionexplanation
createthe document doesn’t exit,then create the document
indexcreate the document or replace the existed document
updateupdate the document
deletedelete the document

the above actions need have three params including _index,_type,_id

{“delete”:{“_index”:”website”,”_type”:”blog”,”_id”:”123”}}

{“create”:{“_index”:”website”,”_type”:”blog”,”_id”:”123”}}
{“title”:”My first blog post”}

the complete bulk request
{“delete”:{“_index”:”website”,”_type”:”blog”,”_id”:”123”}}
{“create”:{“_index”:”website”,”_type”:”blog”,”_id”:”123”}}
{“title”:”My first blog post”}
{“update”:{“_index”:”website”,”_type”:”_type”:”blog”,”_id”:”123”,”_retry_on_conflict”:3}}
…..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值