加载中

Introduction

ElasticSearch is an open-source and distributed search engine which is very much scalable and supports a good amount of enterprise Search use cases. It's built on top of Lucene (just like Apache Solr4). It supports realtime time indexing and full text search. You can read about Elastic Search at:

http://www.elasticsearch.org/ 

It exposes a Java and an HTTP API, which can be used for indexing, searching and most of the configuration.

The very reason for writing this blog about ElasticSearch is that http://www.elasticsearch.org/  is more of a reference type and there are no good quality and complete tutorials available. I had some struggle in terms of making it up and running beyond a basic hello world program. I am sharing my experiences so that it can save some time for audiences who would like to try out ElasticSearch (which is a very powerful suite of product). At the end of this tutorial - you will be having a very basic Elastic Search tutorial - up and running. I will be sharing the link from my PC.

简介

ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例。像Solr4一样,是基于Lucene构建的。支持时间时间索引和全文检索。官网:http://www.elasticsearch.org

它对外提供一系列基于java和http的api,用于索引、检索、修改大多数配置。

写这篇博客的的主要原因是ElasticSearch的网站只有一些简单的介绍,质量不高,缺少完整的教程。我费了好大劲才把它启动起来,做了一些比hello world更复杂一些的工作。我希望通过分享我的一些经验来帮助对ElasticSearch(很强大的哦)感兴趣的人在初次使用它的时候能够节省些时间。学完这篇教程,你就掌握了它的基本操作——启动、运行。我将从我的电脑上分享这个链接。

So let's get started.

  1. I am assuming that you have Java already installed.

  2. Download ElasticSearch from http://www.elasticsearch.org/download/ . Again, there is a lot of talk about using it in Linux and other non-windows environment, but I will be focusing more into Windows 7 Desktop environment. Please choose the installation package accordingly. For Windows - it's a Zip file - one can extract it into C:\elasticsearch-0.90.3\. Remember it's very much like installing Eclipse IDE.

  3. I am new to Curl and cygwin and i wanted to cut short the time frame to learn it (as most of the command references on ElasticSearch.org are for non-Windows platform). You can install Curl from http://curl.haxx.se/download.html and cygwin from http://cygwin.com/install.html 

Now let's test what we have done till now.

  1. In Windows7 Desktop Environment, start command line and cd C:\elasticsearch-0.90.3\bin

  2. Now execute elasticsearch.bat

  3. This will start one of the ElasticSearch nodes on the localhost. You will see the logs somewhat like this

(Please don't worry if it is slightly different in your case, as I have some plugins of Elastic Search and my node names etc... are going to be different from yours)

这么着就开始了。

  1. 作者假设读者拥有安装后的Java。

  2. 下载来自http://www.elasticsearch.org/download/的ElasticSearch。再一次,关于在Linux与其他非视窗系统环境里操作它的谈论有许多,但是作者更加关心着视窗7版桌面环境。请对应选择安装包裹。对视窗系统 - 一Zip文件 - 用户可解压缩到C:\elasticsearch-0.90.3\. 牢记,这十分的不同于安装Eclipse IDE。

  3. 作者不熟悉curl跟cygwin,而且作者打算节省掌握时间(此多数在官网ElasticSearch.org应用的命令面对非视窗平台)(译者:大可以安装一虚拟机、便携版Linux或者MinGW)。读者可以在http://curl.haxx.se/download.htmlhttp://cygwin.com/install.html安装Curl和cygwin。

于是测试下目前作者和读者所做到的。

  1. 视窗7版桌面环境,运行命令行,进入 cd C:\elasticsearch-0.90.3\bin 目录。

  2. 这时运行 elasticsearch.bat

  3. 上面在本机启动了一个ElasticSearch节点。 读者会看到下面的记录提示。

(如果您家情况明显不一样,请读者们不要忧愁,因那作者有些个Elastic Search的插件程序,而且作者家节点命名和其它会不同读者家的)

4. Now test it in your browser

If you get status 200 it means - everything is fine...Isn't it simple?

Let's look at each field of the JSON and see what it's about:

Ok: when it's true, it means that the request was successful.

Status: the HTTP error code that resulted from the request. 200 means OK.

Name: the name of our Elasticsearch instance. By default, it picks a random name from a huge list of names.

Version: The object here has a number field, which is the version of Elasticsearch you're currently running, and a snapshot_build field, which indicates if what you're running has been built from sources.

Tagline: this contains the first tagline of Elasticsearch: “You Know, for Search.”

4. 现在在浏览器里测试一下

如果你得到的status是200那它意味着所有的事情都ok啦...是不是很简单?

让我们看看JSON的每个字段代表的含义:

Ok:当为true时,意味着请求成功。

Status:发出请求后的HTTP的错误代码。200表示一切正常。

Name:我们Elasticsearch实例的名字。在默认情况下,它将从一个巨长的名字列表中随机选择一个。

Version:这个对象有一个number字段,代表了当前运行的Elasticsearch版本号,和一个Snapshot_build字段,代表了你当前运行的版本是否是从源代码构建而来。

Tagline:包含了Elasticsearch的第一个tagline: "You Know, for Search."

5. Now let's install one of the elasticsearch plugin viz. ElasticSearch Head from http://mobz.github.io/elasticsearch-head/ 

It's very simple to install this plug-in.

cd C:\elasticsearch-0.90.3\bin
plugin -install mobz/elasticsearch-head

This will install elasticsearch-head plugin into your environment

Tutorial Sample

We will be developing a very simple application - Employees within a department - so that we can focus on the functionality more than example complexity. After all, this blog is to help somebody jump start with ElasticSearch.

1) Now open up your cygwin window and enter this command

curl -XPUT 'http://localhost:9200/dept/employee/32' -d '{ "empname": "emp32"}'

where dept is a index and index type is employee and we are inputting 31st id for this index type.

You should see something like this in cygwin window:

Go thru this output:

========================================================================
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    91  100    70  100    21    448    134 --:--:-- --:--:-- --:--:--   500{"ok":true,"_index":"dept","_type":"employee","_id":"31","_version":1}
========================================================================

Following the above command - please enter some more records:

curl -XPUT 'http://localhost:9200/dept/employee/1' -d '{ "empname": "emp1"}'
curl -XPUT 'http://localhost:9200/dept/employee/2' -d '{ "empname": "emp2"}'
...
...
curl -XPUT 'http://localhost:9200/dept/employee/30' -d '{ "empname": "emp30"}'

Note : you need to increment the index counter and value of empname within the curly braces.

Once this is done - you have entered enough data for ElasticSearch and you can start searching your data using plugin head.

5. 现在让我们从http://mobz.github.io/elasticsearch-head/ 安装ElasticSearch Head插件

安装方法非常简单

cd C:\elasticsearch-0.90.3\bin
plugin -install mobz/elasticsearch-head

上面的命令会把 elasticsearch-head插件装到你的环境里

教程样例

我们将要部署一个非常简单的应用--在一个部门里的雇员--这样我们可以把注意力放在功能而不是氧立得复杂性上。总而言之,这篇博文是为了帮助人们开始ElasticSearch入门。

1)现在打开你的cygwin窗口并且键入命令

curl -XPUT 'http://localhost:9200/dept/employee/32' -d '{ "empname": "emp32"}'

dept是一个索引并且索引类型是雇员,此时我们正在输入这个索引类型的第31个id。

你应该能在cygwin的窗口看到这样的信息:

让我们看一下这个输出:

========================================================================
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    91  100    70  100    21    448    134 --:--:-- --:--:-- --:--:--   500{"ok":true,"_index":"dept","_type":"employee","_id":"31","_version":1}
========================================================================

和上面的命令一样--让我们输入更多的记录:

curl -XPUT 'http://localhost:9200/dept/employee/1' -d '{ "empname": "emp1"}'
curl -XPUT 'http://localhost:9200/dept/employee/2' -d '{ "empname": "emp2"}'
...
...
curl -XPUT 'http://localhost:9200/dept/employee/30' -d '{ "empname": "emp30"}'

注意:你要记得增加索引计数器和大括号里empname的值。

一旦这些工作都完成了--你为ElasticSearch输入了足够多的数据,你就可以开始使用head插件搜索你的数据了。

Let's test it out

Please type this into the browser:

http://localhost:9200/_plugin/head/ 

It should look something like this:

This is the overview which talks about cluster health and various indexes. Our newly created index is also getting displayed as dept

Now click on the Structured Query tab

In the Search dropdown - select "dept" and then click on "Search" button.

This will displays all the records.

让我们试试吧!

在浏览器中输入:

http://localhost:9200/_plugin/head/ 

你会看到这个:

这里是有关簇使用情况和不同索引信息的概况。我们最近创建的索引在其中,显示为"dept"。

现在点击Structured Query选项卡

在Search下来菜单中选择"dept"并点击"Search"按钮。

这将显示所有记录。

To do a search for specific items

let's say searching for emp1, emp25 and emp7 keep on clicking on the right most "+" to add more into the search criteria as shown in the figure and then click on "Search". Please make sure leftmost is "should" and other search criteria items are as mentioned in the diagram.

can play with this plugin and may be work on your search projects.

You can try out this working application running on my Desktop at:

Now you can play with this plugin and may be work on your search projects.

You can try out this working application running on my Desktop at:

http://98.228.230.117:9200/_plugin/head/ 

Please get back to me in case of any questions or concerns.

Hope this can get you started on a very good Opensource Enterprise Search Product viz. "ElasticSearch".

搜索特定条目

让我们来搜索emp1,emp25和emp7。不断点击最右面的"+"来添加更多的搜索项,就像如图显示的那样,之后点击"Search"。确保最左边的选项为"should",其他的选项也应该和图中的保持一致。

现在你可以继续尝试这个插件了,你可以将其用到你的搜索项目中。

你可以尝试在我的桌面电脑上运行的该应用:

http://98.228.230.117:9200/_plugin/head/ 

如果有任何问题和意见请联系我。

希望这篇文章可以让你快速上手"ElasticSearch"——一个不错的企业级开源搜索产品。

返回顶部
顶部
返回顶部
顶部