1.1.  ElasticSearch(简称ES)

ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,其第一个版本于2010年2月出现在GitHub上并迅速成为最受欢迎的项目之一。

ES的核心不在于Lucene,其特点更多的体现为:

分布式的实时文件存储,每个字段都被索引并可被搜索

分布式的实时分析搜索引擎

可以扩展到上百台服务器,处理PB级结构化或非结构化数据

高度集成化的服务,你的应用可以通过简单的 RESTful API、各种语言的客户端甚至命令行与之

交互。

上手Elasticsearch非常容易。它提供了许多合理的缺省值,并对初学者隐藏了复杂的搜索引擎理论。它拥有开瓶即饮的效果(安装即可使用),只需很少的学习既可在生产环境中使用。

和ES类似的框架

Solr

Solr和ES比较:

Solr 利用 Zookeeper 进行分布式管理,支持更多格式的数据(HTML/PDF/CSV),官方提供的功能更多在传统的搜索应用中表现好于 ES,但实时搜索效率低。

ES自身带有分布式协调管理功能,但仅支持json文件格式,本身更注重于核心功能,高级功能多有第三方插件提供,在处理实时搜索应用时效率明显高于 Solr。

Katta

基于 Lucene 的,支持分布式,可扩展,具有容错功能,准实时的搜索方案。

优点:开箱即用,可以与 Hadoop 配合实现分布式。具备扩展和容错机制。

缺点:只是搜索方案,建索引部分还是需要自己实现。在搜索功能上,只实现了最基本的需求。成功案例较少,项目的成熟度稍微差一些。

HadoopContrib

Map/Reduce 模式的,分布式建索引方案,可以跟 Katta 配合使用。

优点:分布式建索引,具备可扩展性。

缺点:只是建索引方案,不包括搜索实现。工作在批处理模式,对实时搜索的支持不佳。

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

ES数据管理

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

创建索引文档

①使用自己的ID创建:

PUT {index}/{type}/{id}

{

"field": "value",

...

}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

局部更新文档

接受一个局部文档参数 doc,它会合并到现有文档中,对象合并在一起,存在的标量字段被覆盖,新字段被添加。

POST itsource/employee/123/_update

{

“doc”:{

"email" : "nixianhua@itsource.cn",

"salary": 1000

}

}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

删除文档

DELETE {index}/{type}/{id}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

批量操作bulk  API

使用单一请求来实现多个文档的create、index、update 或 delete。

Bulk请求体格式:

{ action: { metadata }}\n

{ request body }\n

{ action: { metadata }}\n

{ request body }\n

POST _bulk
{ "delete": { "_index": "itsource", "_type": "employee", "_id": "123" }}
{ "create": { "_index": "itsource", "_type": "blog", "_id": "123" }}
{ "title": "我发布的博客" }
{ "index": { "_index": "itsource", "_type": "blog" }}
{ "title": "我的第二博客" }

批量获取

#批量获取方式一
GET _mget
{
"docs":[{
"_index":"itsource",
"_type":"blog",
"_id":"123"
},{
"_index":"itsource",
"_type":"blog",
"_id":"AWpXiEfhCq6ubXlpA9Ia",
"_source":"title" }]
} #批量获取方式二
GET itsource/blog/_mget
{
"ids":["123","AWpXiEfhCq6ubXlpA9Ia"]
}

分页查询

#分页查询
GET _search?size=3&from=2;
//查询条件位欸age=18的
GET crm/employees/_search?q=age:18
//查询10>age<30
GET crm/employees/_search?q=age[10 TO 30]

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman",serif;}

DSL查询

#DSL的查询方式
GET crm/employees/_search
{
"query" : {
"match" : {
"name" : "大哥"
}
}
}

案例:类似京东网站 查询关键字为iphone,国家为us的,价格范围6000到8000 价格降序,并# 且取前面2条:

GET shop/goods/_search
{
"query":{
"bool": {
"must": [
{"match": {
"name": "iphone"
}}
],
"filter": [{
"term":{
"local":"us"
}
},{
"range":{
"price":{
"gte":"5000",
"lte":"7000"
}
}
}]
}
},
"from": 1,
"size": 5,
"_source": ["id", "name", "type","price"],
"sort": [{"price": "desc"}]

在java中的操作为

 public class elasticTest {

     //取得clean对象
public TransportClient getClient() throws UnknownHostException {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client; }
//添加数据
@Test
public void getCreated() throws Exception {
TransportClient client = getClient();
//创建一个库
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("crm", "user", "1");
//准备数据
Map<String, Object> mp = new HashMap();
mp.put("id", 2);
mp.put("name", "kg");
mp.put("age", 18);
//将数据放入到库中,并且将数据读取出来
IndexResponse indexResponse = indexRequestBuilder.setSource(mp).get();
System.out.println(indexResponse);
}
//修改数据
@Test
public void update() throws Exception{
TransportClient client = getClient();
Map mp=new HashMap();
mp.put("id", 2);
mp.put("name", "黄巢");
mp.put("age", 35);
UpdateResponse response = client.prepareUpdate("crm", "user", "1").setDoc(mp).get();
GetResponse fields = client.prepareGet("crm", "user", "1").get();
System.out.println(fields.getSource()); }
//进行删除
@Test
public void testdelete() throws Exception{
TransportClient client = getClient();
DeleteResponse response = client.prepareDelete("crm", "user", "1").get();
System.out.println(response);
}
//批量添加
@Test
public void BUlk() throws Exception{
TransportClient client = getClient();
BulkRequestBuilder bulk = client.prepareBulk();
for (int i=0;i<10;i++){
Map map=new HashMap();
map.put("id", i);
map.put("age", 6+i);
map.put("name", "zhansan"+i); bulk.add(client.prepareIndex("crm","suer",i+"").setSource(map));
}
BulkResponse response = bulk.get(); SearchRequestBuilder search = client.prepareSearch("crm", "suer");
System.out.println(search); if (response.hasFailures()){
System.out.println("err");
}
client.close();
} @Test
public void testQuery() throws Exception{
TransportClient client = getClient();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//匹配值
List<QueryBuilder> must = boolQuery.must();
must.add(QueryBuilders.termQuery("name", "zhansan1")); //过滤
List<QueryBuilder> filter = boolQuery.filter();
filter.add(QueryBuilders.rangeQuery("age").gte("6").lte(10)); //设置分页
SearchResponse response = client.prepareSearch("crm")
.setFrom(0).setSize(3)
.setQuery(boolQuery)
.addSort("id", SortOrder.DESC).get(); System.out.println("总条数"+response.getHits().getTotalHits());
//第一次gethits表示获取到命中条数,第二次表示获取得到命中条数的数组
SearchHit[] hits = response.getHits().getHits();
//遍历数组得到具体的值
for (SearchHit hit : hits) {
System.out.println(hit.getSource());
} }
}

ElasticSearch的基本认识和基本操作的更多相关文章

  1. 【ELK】【ElasticSearch】3.es入门基本操作

    docker安装elasticSearch步骤 ================================================================== 本篇参考: htt ...

  2. Elasticsearch 7.x文档基本操作(CRUD)

    官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html 1.添加文档 1.1.指定文档ID PUT ...

  3. Elasticsearch 学习总结 - 相关配置补充说明

    一.   Elasticsearch的基本概念 term索引词,在elasticsearch中索引词(term)是一个能够被索引的精确值.foo,Foo Foo几个单词是不相同的索引词.索引词(ter ...

  4. 【ELK】【docker】6.Elasticsearch 集群启动多节点 + 解决ES节点集群状态为yellow

    本章其实是ELK第二章的插入章节. 本章ES集群的多节点是docker启动在同一个虚拟机上 ====================================================== ...

  5. 【ELK】【docker】【elasticsearch】1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安装ik分词器

    系列文章:[建议从第二章开始] [ELK][docker][elasticsearch]1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安 ...

  6. Elasticsearch系列---生产集群的索引管理

    概要 索引是我们使用Elasticsearch里最频繁的部分日常的操作都与索引有关,本篇从运维人员的视角,来玩一玩Elasticsearch的索引操作. 基本操作 在运维童鞋的视角里,索引的日常操作除 ...

  7. Elasticsearch笔记二之Curl工具基本操作

    Elasticsearch笔记二之Curl工具基本操作 简介: Curl工具是一种可以在命令行访问url的工具,支持get和post请求方式.-X指定http请求的方法,-d指定要传输的数据. 创建索 ...

  8. ElasticSearch 全文检索— ElasticSearch 基本操作

    REST 简介-定义 REST (REpresentation State Transfer)描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding ...

  9. ElasticSearch之安装及基本操作API

    ElasticSearch 是目前非常流行的搜索引擎,对海量数据搜索是非常友好,并且在高并发场景下,也能发挥出稳定,快速特点.也是大数据和索搜服务的开发人员所极力追捧的中间件.虽然 ElasticSe ...

随机推荐

  1. PHP下ajax跨子域的解决方案之document.domain+iframe

    对于主域相同,子域不同,我们可以设置相同的document.domain来欺骗浏览器,达到跨子域的效果.   例如:我们有两个域名:www.a.com 和 img.a.com 在www.a.com下有 ...

  2. binlog怎样参与mysql recover的

    转自  Louis Hust's Blog MySQL两阶段提交 29 July 2015 参数介绍 两阶段提交 什么情况下会出现binlog写入了,但是实际这条数据不存在库中? 参数介绍 innod ...

  3. PAT 1058 选择题(20)(代码+思路)

    1058 选择题(20 分) 批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多. 输入格式: 输入在第一行给出两个正整数 N(≤ 1000)和 M(≤ 100 ...

  4. 设计资源:三个精美APP原型例子下载

    原型设计是整个产品生产过程中不可或缺的一环,无论你是移动端UI设计师或是网页设计师,原型设计都会让整个设计过程更加轻松.原型是产品概念的具象化,它让每个项目参与者都能查看并提出意见以便在产品发布前日臻 ...

  5. 构造函数constructor 与析构函数destructor(三)

    (1)构造函数初始化列表: 1 class Test{ 2 int i; 3 public: 4 Test(int vi):i(vi){}//这里的从冒号开始,到右大括号结束,这一段是构造函数初始化列 ...

  6. 使用delphi 开发多层应用(二十二)使用kbmMW 的认证管理器

    从kbmmw 4.4 开始,增加了认证管理器,这个比原来的简单认证提供了更多的功能.细化了很多权限操作. 今天对这一块做个介绍. 要做一个认证管理,大概分为以下5步: 1.  定义你要保护的资源,一般 ...

  7. 2018.08.22 NOIP模拟 string(模拟)

    string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...

  8. xampp环境 安装 用法 composer

    准备工作 1.打开PHP配置文件E:\xampp\php\php.ini确认以下模块已开启(移除前面的分号). extension=php_openssl.dll, extension=php_cur ...

  9. yii2 控制器的生命周期

    控制器生命周期 http://www.yii-china.com/doc/guide/structure_controllers.html 处理一个请求时,应用主体 会根据请求路由创建一个控制器,控制 ...

  10. ThinkPHP5命令行 执行控制器下的某方法

    入口文件后加一个空格就行了 1,首先cd到站点目录public下,我的入口文件是默认的index.php,然后执行以下命令,, 2,php要加入环境变量,index.php后面加空格,然后是模块,控制 ...