Basic Operation _elasticsearch of Elasticsearch

Source: Internet
Author: User
Tags curl elastic search
Absrtact: This article briefly introduces the insertion, deletion, update, lookup and search functions in the HTTP API of Elasticsearch.

Elasticsearch is an Open-source (Apache2 protocol), distributed, restful search engine built on Apache Lucene.

It has many features such as schema free,document oriented. It is #nosql, based on JSON, and supports a variety of APIs, including HTTP, Thrift, memcached. Support HTTP, is a bit more cool, because basically all the applications can use ES, the page of the JS script can be queried. Installation

Start-up and installation is particularly simple, in the ES download page download zip or tar package, decompression, and then to the Elasticsearch directory, run the following command on it.

Bin/elasticsearch-f

Building clusters is also very simple, in the same network segment of the machine, boot es, they will automatically set up a cluster, and complete the data distributed storage, the query will also be distributed in the way to find.

Well congratulations, now you can build es stand-alone and Es clusters, everything is so simple.

Let's take a look at the ES of the HTTP API insert, DELETE, update, find, search function, (es installed in the Ubuntu Server 64-bit 12.04LTS). Insert

Let's start with a simple official example, insert a parameter for-xput, and insert a record.

$ Curl-xput ' HTTP://LOCALHOST:9200/TWITTER/TWEET/1 '-d ' {
    "user": "Kimchy",
    "post_date": "2009-11-15t14 : 12:12 ", Message
    ": "Trying out elastic Search"
} '

The results of the execution are as follows: The contents of the green box are the result of ES return.

andrew@ubuntu:~$ curl-xput ' HTTP://LOCALHOST:9200/TWITTER/TWEET/1 '-d ' {
> '     user ': ' kimchy ',
>     "post_date": "2009-11-15t14:12:12",
>     "message": "Trying out elastic Search"
>} '
{OK ': True, "_index": "Twitter", "_type": "Tweet", "_id": "1", "_version": 6}andrew@ubuntu:~$

From the example above, you can see the default port 9200 for the ES HTTP service, followed by the/TWITTER/TWEET/1 is the index portion of the record.

This also reflects its restful style, all records are determined by the URI. The three-level directory distribution corresponds to the _index,_type, _id (as can be seen in the green box). In fact, all the records stored on ES can only be found through a level three directory, not much or less.

The _id field can be either a number or a string. ES will automatically create these indexes when the above command is executed. -D followed by a record in JSON format to insert.

-xput indicates that this is inserting a piece of data, ES is called to create an index. The result of the ES return, a _version field that indicates the version number of the current record, and when you want the index to put a record back, the version number is automatically added. Delete

The deleted http request parameter is-xdelete, and you can delete this record by using the following command:

Curl-xdelete ' HTTP://LOCALHOST:9200/TWITTER/TWEET/1 '

When you delete this record, the _verison will also automatically add one. Inquire

Once you have created an index, you can query (parameter-xget) in the following way:

Curl-xget ' HTTP://LOCALHOST:9200/TWITTER/TWEET/1 '

To execute the query command above, you can wait until the following result:

andrew@ubuntu:~$ curl-xget ' http://localhost:9200/twitter/tweet/1 '
{"_index": "Twitter", "_type": "Tweet", "_id" : "1", "_version": 5, "exists": true, "_source": {
    "user": "Kimchy",
    "post_date": "2009-11-15t14:12:12",
    " Message ":" Trying out elastic Search "
}}andrew@ubuntu:~$ 

exists indicates whether there is a query result, and the _source field is the record that is queried.

Query, you can set the _type setting to _all,es to return all the type under _index, the first to match the _id record.

You can also continue to control the return result by using parameters, such as selecting the Returned field with fields, and using pretty to control whether the returned JSON format is more readable and friendly. Format=yaml can set the input format to YAML. Here are two examples

Curl-xget ' http://localhost:9200/twitter/tweet/1?fields=message,user&pretty=true '
curl-xget ' http:// Localhost:9200/twitter/tweet/1?fields=message,user&format=yaml '

Of course, ES also support the query of multiple sets of records, that is, multi get, in the URI is the use of keyword _mget, you can refer to the multi of ES documents. Update

ES also support updates, but the update is done through a provided script. The ES approach is to find the appropriate node for the record by index, then execute the script, and then return to the new index after execution. In fact, a get and reindex process is performed, in which the versioning controls no other update operations (this feature is available after 0.19). The principle of concrete implementation should be related to elasticsearch versioning.

The meaning of Get,reindex is that ES first takes out this record, then generates new records based on new data, and then puts the new records back in ES (and does not overwrite old records).

First create a record

$ curl-xput LOCALHOST:9200/TEST/TYPE1/1-d ' {
    "counter": 1,
    "tags": ["Red"]
} '

Add the value of counter to 4

$ Curl-xpost ' localhost:9200/test/type1/1/_update '-d ' {'
    script ': ' Ctx._source.counter = = Count ',
    ' params ': { '
        count ': 4
    }
} '

You can also add a tag value

$ Curl-xpost ' localhost:9200/test/type1/1/_update '-d ' {
    "script": "Ctx._source.tags + + Tag",
    "params": {
   "tag": "Blue"
    }
} '

Now also supports the Upsert function, that is, if the record does not have this key at the time of the update, insert the key, here is an example, if there is no counter field, insert the field:

$ Curl-xpost ' localhost:9200/test/type1/1/_update '-d ' {'
    script ': ' Ctx._source.counter = = Count ',
    ' params ': {
        ' Count ': 4
    },
    ' Upsert ': {
        ' counter ': 1
    }
} '

There are many other features about update that can be referenced in the ES API update search

Elasticsearch's name has a search, so the main function is search.

The ES search has two forms, one through the URI and the other through the requst body. With a URI query, the statement of the query is placed in the requested URL, for example:

Curl-xget ' Http://localhost:9200/twitter/tweet/_search?q=user:kimchy '

The second way is to include a doc in the query request

$ Curl-xget ' http://localhost:9200/twitter/tweet/_search '-d ' {
    "query": {
        "term": {"user": "Kimchy"}
  
   }
}'

  

Query the definition of the body can see query DSL two other query methods can take parameters, the meaning of the parameter reference URI request and request body.

The ES search function can span both index and type, such as the following commands

Curl-xget ' http://localhost:9200/twitter/_search?q=user:kimchy '
curl-xget ' http://localhost:9200/twitter/ Tweet,user/_search?q=user:kimchy '
curl-xget ' Http://localhost:9200/kimchy,elasticsearch/tweet/_search?q=tag : Wow '
curl-xget ' http://localhost:9200/_all/tweet/\_search?q=tag:wow '
curl-xget ' http://localhost:9200/\ _search?q=tag:wow '

The first is to find all of the type in all Twitter, the second is to look in the two type Tweet,user, and the third is to look up in the kimchy,elasticsearch of the two index tweets. The fourth article uses the _all keyword, which is found in the type of all index tweets, and the fifth is more violent, looking in all the index and type.

There are many other options, sort, highlight, select the domain Fields to return records, you can also use a script for the returned field to compute the script Fields, or more on the return result facets,facets, which supports keyword statistics, In-range statistics, direct-side schema statistics, date-by-side schema statistics, filtering, querying, and statistics geo distance that record geographical distances. Supports name filtering named Filters. Defines the search type. For example what Query and Fetch,query Then Fetch. Index boost, which allows one index to have more weight than the other. Keep the last retrieved environment the result scroll. Preserves the score value of each hit explain. Sets the min_score of the hit. Keep version number versions.

Search parameters are many, I did not look at one, but it is the name inside a search, the search for a variety of scenarios are supported.

Of course, multiple queries are supported multi search, such as the following example

$ cat Requests
{"Index": "Test"}
{"query": {"Match_all": {}}, "from": 0, "size": "
{index": "Test", " Search_type ": Count"}
{"query": {"Match_all": {}}} {
} {
"query": {"Match_all": {}}}

$ curl-xget loca Lhost:9200/_msearch--data-binary @requests; Echo
Summary

These are the basic functions of elasticsearch. Of course it has many other functions, you can go to http://www.elasticsearch.org to see.

ES is based on Lucene, and there are many concepts that come directly from it, and all you need to learn about ES is a little bit of Lucene's foundation.

Overall, feeling elasticsearch is a relatively powerful tool, and the support of social networks is better, and easy to use, simple configuration, do not know how the stability.

Here are two good articles, you can also learn from: http://blog.csdn.net/laigood12345/article/details/7421173 http://www.qwolf.com/?p=1387

There are elasticsearch Chinese site http://www.elasticsearch.cn/, but this site is still in the construction, translation work has just begun, we hope to finish early.


from:http://liuhongjiang.github.io/tech/blog/2013/01/11/es/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.