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
action | explanation |
---|---|
create | the document doesn’t exit,then create the document |
index | create the document or replace the existed document |
update | update the document |
delete | delete 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}}
…..