前言
ElasticSearch是基于全文搜索引擎库Lucene构建的分布式搜索引擎,我们可以直接使用ElasticSearch实现分布式搜索系统的搭建与使用,都知道,Lucene只是一个搜索框架,它提供了搜索引擎操作的基本API,如果要实现一个能够使用的搜索引擎系统,还需要自己基于Lucene的API去实现,工作量很大,而且还需要很好地掌握Lucene的底层实现原理。ElasticSearch是一个完整的分布式搜索引擎系统,它的一些基本特性包括如下:
- 全文检索
- 提供插件机制,可以共享重用插件的功能
- 分布式文件存储
- 分布式实时索引和搜索
- 实时统计分析
- 可以横向扩展,支持大规模数据的搜索
- 简单易用的RESTful API
- 基于Replication实现了数据的高可用特性
- 与其他系统的集成
- 支持结构化和非结构化数据
- 灵活的Schema设计(Mappings)
- 支持多编程语言客户端
我个人感觉,ElasticSearch尽量屏蔽底层Lucene相关的技术细节,让你根本无从感觉底层Lucene相关的内容,这样你可以省去了了解Lucene 的成本,学习曲线比较平缓,不像Solr,如果想要构造负责的查询(Query),还是要对Lucene有所了解的。另外,在分布式设计方面,ElasticSearch更轻量一些,用起来更简单,而使用Solr的分布式分片功能需要使用SolrCloud,它基于ZooKeeper来实现配置管理,以及Replication功能,而且Solr需要使用Web容器来部署,相对来说有点复杂一些(我个人之前使用的SolrCloud版本大概是3.1~3.5左右,比较早,现在可能更加完善了)。
基本概念
我们熟悉一下ElasticSearch中涉及到的一些基本概念:
- 索引(Index)
索引(Index)是文档的集合,它是根据实际业务逻辑进行划分的,通常会把相对独立且具有相似结构或者性质的数据作为文档,放在一起,形成一个索引,比如,用户相关信息可以作为一个索引,交易相关信息也可应作为另一个索引。
- 类型(Type)
类型(Type)是索引内部的一个逻辑划分,在一个索引内部可以定义多个类型(Type),类型将一个索引在逻辑上划分为多个集合,每个类型包含多个属性(字段)。比如,我们基于手机客户端应用App,创建一个了用户相关信息的索引,然后再在这个索引内部定义多个类型:基本信息类型、设备信息类型、行为信息类型,基本信息类型中包含用户编号、证件号码、名称、手机号码、年龄、出生日期,设备信息类型包括设备类型、设备名称、App版本号、渠道来源、系统版本、IMEI、mac地址,用户行为信息包含用户编号、事件编号、事件类型、时间、浏览页面代码、地区编码,这样有3个类型在一个索引当中。ElasticSearch中类型,与HBase中列簇(Column Family)的概念很相似。
- 文档(Document)
文档(Document)是索引的基本单元,它与关系数据库中的一条记录相类似,包含了一组属性信息,同时包含一个唯一标识这一组属性值的ID,通过该ID可以更新一个文档,也可以删除一个文档。
- 分片(Shards)&副本(Replicas)
一个索引是很多文档的集合,将一个索引进行分割,分成多个片段(一个索引的子集),每一个片段称为一个分片(Shard),这样划分可以很好地管理索引,跨节点存储,为分布式存储于搜索提供了便利。副本(Replica)是为了保证一个分片(Shard)的可用性,冗余复制存储,当一个分片对应的数据无法读取时,可以读取其副本,正常提供搜索服务。
集群安装配置
ElasticSearch集群安装配置非常容易,安装可以执行如下命令行:
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.0.0/elasticsearch-2.0.0.zip
unzip elasticsearch-2.0.0.zip
拿出集群的一个节点的进行配置,修改配置文件config/elasticsearch.yml的内容,如下所示:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: dw_search_engine
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: esnode-01
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /data/dw_search_storage
#
# Path to log files:
#
path.logs: /tmp/es/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind adress to a specific IP (IPv4 or IPv6):
#
network.host: 10.10.2.62
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Elasticsearch nodes will find each other via unicast, by default.
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["es-01", "es-02"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true
其它节点的配置,在保证基本存储目录相同的前提下,可以根据需要修改如下几个参数:
node.name
network.host
http.port
最后,在每个节点上分别启动ElasticSearch,执行如下命令:
cd elasticsearch-2.0.0
bin/elasticsearch -d
然后可以查看Web管理界面,需要安装插件elasticsearch-head,后面会介绍,Web管理界面,如下所示:
上图中,我们已经创建了一个索引,可以看到节点的状态,及其分片(Shard)的情况。
RESTful API基本操作
尤其是在进行搜索的时候,为了使得其他系统能够与ElasticSearch搜索系统很好地解耦合,使用ElasticSearch提供的RESTful API是一种不错的选择。下面,我们介绍RESTful API的基本操作。
- 插件管理
插件的存放目录为elasticsearch-2.0.0/plugins/,插件都是基于该存储目录进行操作的。安装插件:
bin/plugin install analysis-icu
bin/plugin install mobz/elasticsearch-head
可以从不同的位置安装插件,上面第一个称为Core Elasticsearch plugin,它是Elasticsearch提供的,会从Elasticsearch上下载并安装;上面第一个是从Github上自动下载安装。还有其他的方式安装,如从特定的文件系统等进行安装。列出插件:
bin/plugin list
删除插件:
bin/plugin remove analysis-icu
安装完一个插件,我们可以查看,例如查看elasticsearch_head插件,查看如下链接:
http://10.10.2.62:9200/_plugin/head/
- 创建索引
curl -XPUT 'http://10.10.2.62:9200/basis_device_info/'
创建的索引名称为basis_device_info,我们也可以不指定一个索引对应的Mappings,而是在索引的时候自动生成Mappings,所以如果没有指定一个索引的Mappings,则这个索引可以支持任何的Mappings。同样可知,一个索引可以自动地增加不同的type,非常灵活。也可以指定索引的基本配置,如分片(Shard)数目、副本(Replica)数目,如下所示:
curl -XPUT 'http://10.10.2.62:9200/basis_device_info /' -d '{
"settings" : {
"index" : {
"number_of_shards" : 10,
"number_of_replicas" : 1
}
}
}'
默认是5个分片,不进行复制,上面配置表示索引basis_device_info有10个分片,每个分片1个副本。下面在创建索引的时候,指定设计的schema,即配置mappings,如下所示:
curl -XPUT 'http://10.10.2.62:9200/basis_device_info/' -d '
{
"mappings": {
"user": {
"_all": { "enabled": false },
"properties": {
"installid": { "type": "string" },
"appid": { "type": "string" },
"channel": { "type": "string", "index": "analyzed" },
"version": { "type": "string" },
"osversion": { "type": "string" },
"device_name": { "type": "string", "index": "analyzed" },
"producer": { "type": "string" },
"device_type": { "type": "string" },
"resolution": { "type": "string", "index": "analyzed" },
"screen_size": { "type": "string", "index": "analyzed" },
"mac": { "type": "string", "index": "not_analyzed" },
"idfa": { "type": "string" },
"idfv": { "type": "string", "index": "not_analyzed" },
"imei": { "type": "string", "index": "not_analyzed" },
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss",
"index": "not_analyzed"
}
}
}
}
}'
上面创建了索引basis_device_info,同时type为user,有了mappings,我们就知道需要索引的数据的格式了。
- 删除索引
curl -XDELETE 'http://10.10.2.62:9200/basis_device_info/'
删除索引basis_device_info。
- 索引文档
curl -PUT 'http://10.10.2.62:9200/basis_device_info/user/CC49E748588490D41BFB89584007B0FA' -d '{
"installid": "0000000L",
"appid": "0",
"udid": "CC49E748588490D41BFB89584007B0FA",
"channel": "wulei1",
"version": "3.1.2",
"osversion": "8.1",
"device_name": "iPhone Retina4 Simulator",
"producer": "apple",
"device_type": "1",
"resolution": "640*1136",
"screen_size": "320*568",
"mac": "600308A20C5E",
"idfa": "dbbbs-fdsfa-fafda-321saf",
"idfv": "4283FAE1-19EB-4FA9-B739-8148F76BC8C3",
"imei": "af-sfd0fdsa-fad-ff",
"create_time": "2015-01-14 20:32:05"
}'
基于我们前面创建的type为user的索引,索引一个文档,文档_id为CC49E748588490D41BFB89584007B0FA,文档内容为一个用户设备信息,使用JSON格式表示。
- 批量索引
批量索引,可以根据自己熟悉的编程语言或者脚本来实现,ElasticSearch也提供了一些客户端库。下面我们首先根据数据文件,构造成ElasticSearch索引支持的JSON格式,导出文件,然后通过curl工具去进行批量索引,实际上使用的是ElasticSearch提供的bulk API来实现的。首先处理原始带索引数据,代码如下所示:
package org.shirdrn.es;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import net.sf.json.JSONObject;
import com.google.common.base.Throwables;
public class EsIndexingClient {
public static void closeQuietly(Closeable... closeables) {
if(closeables != null) {
for(Closeable closeable : closeables) {
try {
closeable.close();
} catch (Exception e) { }
}
}
}
public static void main(String[] args) {
String f = "C:\\Users\\yanjun\\Desktop\\basis_device_info.txt";
String out = "C:\\Users\\yanjun\\Desktop\\basis_device_info.json";
File in = new File(f);
BufferedReader reader = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(out));
reader = new BufferedReader(new FileReader(in.getAbsoluteFile()));
String line = null;
while((line = reader.readLine()) != null) {
String[] a = line.split("\t", -1);
if(a.length == 16) {
String udid = a[2];
JSONObject c = new JSONObject();
c.put("_index", "basis_device_info");
c.put("_type", "user");
c.put("_id", udid);
JSONObject index = new JSONObject();
index.put("index", c);
JSONObject doc = new JSONObject();
doc.put("installid", a[0]);
doc.put("appid", a[1]);
doc.put("udid", a[2]);
doc.put("channel", a[3]);
doc.put("version", a[4]);
doc.put("osversion", a[5]);
doc.put("device_name", a[6]);
doc.put("producer", a[7]);
doc.put("device_type", a[8]);
doc.put("resolution", a[9]);
doc.put("screen_size", a[10]);
doc.put("mac", a[11]);
doc.put("idfa", a[12]);
doc.put("idfv", a[13]);
doc.put("imei", a[14]);
doc.put("create_time", a[15]);
writer.write(index.toString() + "\n");
writer.write(doc.toString() + "\n");
}
}
} catch (Exception e) {
throw Throwables.propagate(e);
} finally {
closeQuietly(reader, writer);
}
}
}
运行代码,输出的数据文件为basis_device_info.json,该文件的格式了,示例如下所示:
{"index":{"_index":"basis_device_info","_type":"user","_id":"1c207122a4b2c9632212ab86bac10f60"}}
{"installid":"00000002","appid":"0","udid":"1c207122a4b2c9632212ab86bac10f60","channel":"itings","version":"3.1.1","osversion":"4.1.2","device_name":"Lenovo P770","producer":"Lenovo","device_type":"0","resolution":"540*960","screen_size":"4.59","mac":"d4:22:3f:83:17:06","idfa":"","idfv":"","imei":"861166023335745","create_time":"2015-01-14 19:39:35"}
{"index":{"_index":"basis_device_info","_type":"user","_id":"FA6B1B98E6FF4E6994A1505A996F6102"}}
{"installid":"00000003","appid":"0","udid":"FA6B1B98E6FF4E6994A1505A996F6102","channel":"appstore","version":"3.1.1","osversion":"8.1.2","device_name":"iPhone 6Plus","producer":"apple","device_type":"1","resolution":"640*1136","screen_size":"320*568","mac":"020000000000","idfa":"84018625-A3C9-47A8-88D0-C57C12F80520","idfv":"9D1E2514-9DC8-47A8-ABD0-129FC0FB3171","imei":"","create_time":"2015-01-14 19:41:21"}
{"index":{"_index":"basis_device_info","_type":"user","_id":"8c5fe70b2408f184abcbe4f34b8f23c3"}}
{"installid":"00000004","appid":"0","udid":"8c5fe70b2408f184abcbe4f34b8f23c3","channel":"itings","version":"3.1.1.014","osversion":"4.2.2","device_name":"2014011","producer":"Xiaomi","device_type":"0","resolution":"720*1280","screen_size":"4.59","mac":"0c:1d:af:4f:48:9f","idfa":"","idfv":"","imei":"865763025472173","create_time":"2015-01-14 19:46:37"}
奇数编号行的内容为索引的指令信息,包括索引名称(_index)、类型(_type)、唯一标识(_id),偶数编号行的内容为实际待索引的文档数据。然后,通过curl命令来进行批量索引,执行如下命令:
curl -s -XPOST http://10.10.2.62:9200/basis_device_info/_bulk --data-binary "@basis_device_info.json"
- 搜索文档
简单的搜索,可以通过GET方式搜索,如下所示:
http://10.10.2.62:9200/basis_device_info/user/CC49E748588490D41BFB89584007B0FA
http://10.10.2.62:9200/basis_device_info/user/_search?q=channel:B-hicloud
上面第一个根据唯一的_id进行搜索,结果返回0个或者1个文档;第二个通过指定GET方式参数,其中_search和q是ElasticSearch内置的接口关键字,通过指定字段名称和搜索关键词的方式进行搜索,结果以JSON格式返回。
- Request Body搜索
可以设置请求的body内容,能够支持更加复杂的查询条件然后请求搜索,如下所示:
curl -XGET 'http://10.10.2.245:9200/basis_device_info/user/_search' -d '{
"query" : {
"term" : { "udid": "bc0af2ca66a96725b8b0e0056d4213b6" }
}
}'
结果示例,如下所示:
{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":9.45967,"hits":[{"_index":"basis_device_info","_type":"user","_id":"bc0af2ca66a96725b8b0e0056d4213b6","_score":9.45967,"_source":{"installid":"00000FPq","appid":"0","udid":"bc0af2ca66a96725b8b0e0056d4213b6","channel":"B-hicloud","version":"3.1.1","osversion":"4.4.2","device_name":"H60-L02","producer":"HUAWEI","device_type":"0","resolution":"720*1184","screen_size":"4.64","mac":"ec:cb:30:c4:93:e3","idfa":"","idfv":"","imei":"864103021536104","create_time":"2015-01-18 01:29:16"}}]}}
- 基于Lucene查询语法搜索
如果熟悉Lucene查询(Query),可以构造通过构造复杂的Term关系字符串来进行搜索,示例如下所示:
curl -XGET 'http://10.10.2.62:9200/basis_device_info/user/_search' -d '
{
"query": {
"query_string": { "query": "(channel:baidu OR device_name:HUAWEI)" }
}
}'
查询query字符串的含义是:从channel字段搜索baidu,从device_name字段搜索HUAWEI,然后两者取并集,这实际上一个布尔查询,返回最终结果。
- 使用multi_match搜索
ElasticSearch支持给定搜索关键词,从多个字段中进行搜索,示例如下所示:
curl -XGET 'http://10.10.2.62:9200/basis_device_info/user/_search' -d '
{
"query": {
"multi_match" : {
"query": "HTC",
"fields": [ "channel", "device_name" ]
}
}
}'
这样,只要在channel和device_name两个字段中出现关键词HTC,则都返回结果,结果应该是两个字段匹配上的文档集合的并集。
- 支持Filter搜索
可以在制定Filter进行搜索。例如下面是一个按照时间范围进行过滤,得到搜索结果的查询:
curl -XGET 'http://10.10.2.62:9200/basis_device_info/user/_search' -d '
{
"query": {
"filtered": {
"query": { "match_all": {} },
"filter" : {
"range" : {
"create_time" : { "from" : "2015-01-16 00:00:00", "to" : "2015-01-16 23:59:59" }
}
}
}
}
}'
- 分页搜索
ElasticSearch支持分页搜索,可以通过在RESTful连接中指定size和from参数,来进行分页搜索,如下所示:
curl -XGET 'http://10.10.2.62:9200/basis_device_info/user/_search?size=10&from=20' -d '
{
"query": {
"filtered": {
"query": { "match_all": {} },
"filter" : {
"range" : {
"create_time" : { "from" : "2015-01-16 00:00:00", "to" : "2015-01-16 23:59:59" }
}
}
}
}
}'
上面搜索的含义是:按照时间范围搜索,从第20个文档开始,返回10个文档,相当于一页取10个文档。(原创:时延军(包含链接:http://shiyanjun.cn)
本篇分为上篇,下篇为java客户端运行内容,等待明天更新~~~~~
可以转发关注小编,每天更新技术好文~~~感谢大家支持!!!