尝试使用Elasticsearch

有一篇关于尝试使用Elasticsearch的文章。

环境:Ubuntu 15.10 / Elasticsearch 2.2.0

Elasticsearch是一个全文搜索引擎数据库。
它提供了使用curl发送请求并返回JSON的接口,还有一些插件,可以自动配置集群,非常方便和强大。

安装

首先安装Java。

下一步是安装Elasticsearch。

从官方网站的存储库页面复制粘贴,网址是https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html。

wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add

echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list

sudo apt-get update && sudo apt-get install elasticsearch

启动

使用命令来启动。

sudo /etc/init.d/elasticsearch start
sudo /etc/init.d/elasticsearch restart

安装插件

Elasticsearch 内置了插件安装命令。有各种各样的插件可供使用,如日语全文搜索插件。

安装kuromoji需要。

sudo /usr/share/elasticsearch/bin/plugin install analysis-kuromoji

插件设置将在后面提到。

暂时先确认当前情况。

有一个叫做”集群”和”节点”的词出现。
“集群”是由多个”节点”组成的集合。
当连接带有相同集群名称的elasticsearch环境(节点),它们会自动以集群配置方式进行操作。

查询Elasticsearch的状态。

curl -XGET http://localhost:9200/  

确认指数

curl 'localhost:9200/_cat/indices?v'

查看节点的状态

curl 'localhost:9200/_cat/nodes?v'

确认集群状态(使用_cat API)

curl 'localhost:9200/_cat/health?v'

用浏览器检查状态

在浏览器上可以确认比较方便。
通过安装mobz/elasticsearch-head插件,可以在浏览器上确认状态。

安装

sudo /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

然后在浏览器中访问http://localhost:9200/_plugin/head。

进行设置

更改设置的方法有几种,包括修改配置文件的方法,映射时设置的方法以及启动时指定的方法。

在配置文件中进行设置。

配置文件位于/etc/elasticsearch/elasticsearch.yml等位置。

试着以母语中文来改写如下:

cluster.name: my-application
node.name: node-1
index.number_of_shards: 1
index.number_of_replicas: 0
index.analysis.analyzer.default.type: custom
index.analysis.analyzer.default.tokenizer: kuromoji_tokenizer

设置了集群名称、节点名称、分片数量、副本数量和默认分析器。
如果将默认分析器设置为kuromoji,则可以自动进行日文全文搜索。
实际上,对于实际产品,每个字段可能需要详细的设置,这将在映射中进行配置。

修改了设置文件后,重新启动elasticsearch以使更改生效。

映射

可以将其类似于模式定义,可以设置Tokenizer的配置,可以为每个字段设置类型和搜索配置。

以以下方式发送。以下仅提供简单设置为字符串类型的示例。

curl -XPOST localhost:9200/tutorial -d '{
    "mappings" : {
        "helloworld" : {
            "properties" : {
                "message" : {
                  "type" : "string"
                  },
                "name" : {
                  "type" : "string"
                }
            }
        }
    }
}'

当我们进行分析器和搜索设置时,就可以使每个记录都能实现以下匹配:
寿司がおいしいね → 寿司 おいしい
飲み → 飲む
寿司 → スシ
サーバー → サーバ

检查映射。当索引为tutorial且类型为helloworld时。

curl -XGET 'localhost:9200/tutorial/_mapping/helloworld/?pretty'

将数据存储在索引中

可以使用索引来存储数据。
出现了index、type、document这样的词语,
形成了index/type/document的结构。

如果要以tutorial为索引名,helloworld为类型,以{message, name}作为数据结构保存文档,可以按以下方式操作。
(即使不进行映射,elastic也会自动为数据添加类型。可以通过设置来禁用此功能)

curl -X POST 'http://localhost:9200/tutorial/helloworld/' -d '{ "message": "Hello World! こんにちは!" ,"name" : "hoge"}'

可以在浏览器中确认。

要删除整个索引,请使用以下命令。

删除本地主机上端口为9200的Elasticsearch教程索引。

要完全删除DB的所有内容,
可以使用以下命令:
curl -XDELETE ‘http://localhost:9200/*’ {“acknowledged”:true}%

查询

查询。
要查找完全匹配的字符串,可以采用以下方法。
我正在使用python的elasticsearch客户端。

# -*- coding:utf-8 -*-
from elasticsearch import Elasticsearch
import json
es = Elasticsearch("localhost:9200")

matchstring = es.search(index="tutorial",
                        doc_type="helloworld",
                        body={
                            "query" : {
                                "match" : {
                                    "name" : "hoge"
                                }
                            }
                        })

使用simple_query_string作为字符串搜索的便利函数时。

simpleString = es.search(index="tutorial",
                         doc_type="helloworld",
                         body={
                            "query" : {
                                "simple_query_string" :{
                                    "query" : "こんにちは",
                                    "fields" : ["message"]
                                }
                            }
                         })

要执行”and”、”or”和”を行うには”操作,似乎需要使用”must”或”should”查询。”name”在”hoge”中同时”message”中有”Hello”,似乎是这样的。

queryBool = es.search(index="tutorial",
                      doc_type="helloworld",
                      body={
                        "query":{
                            "bool":{
                                "must":{
                                    "must":{
                                        "term" : {
                                            "name" : "hoge"
                                        }
                                    },
                                    "term" : {
                                        "message" : "Hello"
                                    }
                                }
                            }
                        }
                      })

还有其他的筛选和汇总查询。
使用查询似乎可以进行基于得分的搜索。

参考和拓展

了解Elasticsearch的概念

Elasticsearch教程

弹性实用设置和查询
http://engineer.wantedly.com/2014/02/25/elasticsearch-at-wantedly-1.html

设置kuromoji_tokenizer
https://github.com/elastic/elasticsearch-analysis-kuromoji

ElasticSearch分析器的设置

只需要一个选项,用中文重新表述以下内容:

使用Python客户端开始“Elasticsearch初级指南”
http://qiita.com/ikawaha/items/c654f746cfe76b888a27

在这篇备忘录中,将使用Elasticsearch和kuromoji进行准确的日语全文搜索。
技术链接:http://tech.gmo-media.jp/post/70245090007/elasticsearch-kuromoji-japanese-fulltext-search

广告
将在 10 秒后关闭
bannerAds