场景:1 互联网搜索:谷歌、百度、各种新闻首页2 站内搜索(垂直搜索):企业 OA 查询订单、人员、部门,电商网站内部搜索商品(淘宝、京东)场景。
1 总结2 1 、 es 基本是开箱即用 ( 解压就可以用 !) 【南京】 , 非常简单。 Solr安装略微复杂一丢丢 !3 2 、 Solr 利用 Zookeeper 进行分布式管理 , 而Elasticsearch 自身带有分布式协调管理功能 。4 3 、 Solr 支持更多格式的数据 , 比如 JSON 、 XML 、 CSV , 而Elasticsearch 仅支持 json 文件格式。5 4 、 Solr 官方提供的功能更多 , 而 Elasticsearch 本身更注重于核心功能,高级功能多有第三方插件提供,例如图形化界面需要 kibana 友好支撑6 5.Solr 查询快 , 但更新索引时慢 ( 即插入删除慢 ) ,用于电商等查询多的应用 ;78 ES 建立索引快 ( 即查询慢 ) ,即实时性查询快,用于 facebook 新浪等搜索。9 Solr 是传统搜索应用的有力解决方案,但 Elasticsearch 更适用于新兴的实时搜索应用。10 6 、 Solr 比较成熟,有一个更大,更成熟的用户、开发和贡献者社区,而 Elasticsearch 相对开发维护者较少 , 更新太快 , 学习使用成本较高。
3、Elasticsearch
The Elastic Stack, 包括 Elasticsearch 【搜索,分析】、 Kibana 【可视 化】、 Beats 和 Logstash 【数据的搜集】(也称为 ELK Stack )。能够安 全可靠地获取任何来源、任何格式的数据,然后实时地对数据进行搜索、 分析和可视化。Elaticsearch ,简称为 ES , ES 是一个 开源的高扩展的分布式全文搜索引 擎 , 是整个 ElasticStack 技术栈的核心。它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理 PB 级别的数据。
1 、安装 JDK ,至少 1.8.0_73 以上版本,验证:java -version 。2 、下载和解压缩 Elasticsearch 安装包,查看目录结构。https://www.elastic.co/cn/downloads/elasticsearch下载地址: https://www.elastic.co/cn/downloads/历史版本下载: https://www.elastic.co/cn/downloads/past-releases/解压后,进入 bin 文件目录,点击 elasticsearch.bat 文件启动 ES 服务 。注意: 9300 端口为 Elasticsearch 集群间组件的通信端口, 9200 端口为浏览器访问的 http 协议 RESTful 端口。打开浏览器,输入地址: http://localhost:9200 ,测试返回结果,返回结果如下

1 、 kibana 是 es 数据的前端展现,数据分析时,可以方便地看到数据。作为开发人员,可以方便访问 es 。https://www.elastic.co/cn/downloads/历史版本下载: https://www.elastic.co/cn/downloads/past-releases/2 、下载,解压 kibana 。3 、启动 Kibana : bin\kibana.bat4 、浏览器访问 http://localhost:5601 进入 Dev Tools 界面。像 plsql 一样支持代码提示。5 、发送 get 请求,查看集群状态 GET _cluster/health 。相当于浏览器访问
6、基本Rest命令说明 
PUT /索引名称/类型名称/1
{
数据
}
创建索引并往索引中添加一条文档(1)
PUT / test1 / type1 / 1{"name" : "张三 " ,"age" : 18}(2)创建索引---但是不添加数据。
PUT /索引名/类型
PUT /qy150
{
"mappings":{
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
}
}
}
}
DELETE /索引名 删除索引
DELETE /索引名/文档名/文档id 删除某一项

GET /_cat/indices?v

GET /索引名

# 必须指定id的值
PUT /qy151/student/2
{
"name":"李四",
"age":25
}# 不指定id
POST /qy151/student/
{
"name":"王五",
"age": 32
}// 如果没有将文档中的所有字段都写,修改后,没写的字段就会丢失POST /qy151/student/{"name" : " 流柚 "}//解决办法POST /qy151/student/_update{"doc":{"name" : " 流柚 ",}

GET /user/_doc/_search 其中的_doc 可以省略不写
GET /user/_doc/_search?q=name:流
match :匹配(会使用分词器解析(先分析文档,然后进行查询))_source :过滤字段sort :排序form 、 size 分页
// 查询匹配GET /blog/user/_search{"query":{"match":{"name":" 流 "}},"_source": ["name","desc"] //限制只有name,desc这两个字段进行输出,"sort": [ //sort 排序{"age": {"order": "asc"}}],


// 精确查询(必须全部都有,而且不可分,即按一个完整的词查询)// term 直接通过 倒排索引 指定的词条 进行精确查找的GET /blog/user/_search{"query":{"term":{"desc":" 年 "}}}
must 相当于 andshould 相当于 ormust_not 相当于 not (... and ...)filter 过滤
(1)must

(2)should
(3)must_not

text :支持分词 , 全文检索 、支持模糊、精确查询 , 不支持聚合 , 排序操作 ;text 类型的最大支持的字符长度无限制 , 适合大字段存储;keyword :不进行分词 , 直接索引 、支持模糊、支持精确匹配,支持聚合、排序操作。keyword 类型的最大支持的长度为 ——32766 个 UTF-8 类型的字符 ,可以通过设置 ignore_above 指定自持字符长度,超过给定长度后的数据将不被索引, 无法通过 term 精确匹配检索返回结果 。
// 测试 keyword 和 text 是否支持分词// 设置索引类型PUT /test{"mappings": {"properties": {"text":{"type":"text"},"keyword":{"type":"keyword"}}}}// 设置字段数据PUT /test/_doc/1{"text":" 测试 keyword 和 text 是否支持分词 ","keyword":" 测试 keyword 和 text 是否支持分词 "}// text 支持分词// keyword 不支持分词GET /test/_doc/_search{"query":{"match":{"text":" 测试 "}}}// 查的到GET /test/_doc/_search{"query":{"match":{"keyword":" 测试 "}}}// 查不到,必须是 " 测试 keyword 和 text 是否支持分词 " 才能查到GET _analyze{"analyzer": "keyword","text": [" 测试 流"]}// 不会分词,即 测试流GET _analyze{"analyzer": "standard","text": ["测试流"]}// 分为 测 试 流

(1)创建一个Springboot工程并加入相关的依赖
创建springboot

com.alibaba
fastjson
1.2.75
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
(2) 创建一个配置,获取ES工具类对象。
- @Configuration
- public class ESConfig {
-
- //该对象可以对我们的ES进行相关的操作
- @Bean
- public RestHighLevelClient restHighLevelClient(){
- RestHighLevelClient client = new RestHighLevelClient(
- RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
- return client;
- }
- }

(3)对ES操作
- @Autowired
- private RestHighLevelClient client
- //PUT /索引名称
- @Test
- void testCreateIndex() throws Exception {
- //该类把创建索引的信息都封装到该类中
- CreateIndexRequest createIndexRequest=new CreateIndexRequest("qy151-index");
- CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
- System.out.println(createIndexResponse.isAcknowledged());
- }
- @Autowired
- private RestHighLevelClient client
- @Test
- public void testDeleteIndex() throws Exception {
- DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy151-index");
- AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest,
-
- RequestOptions.DEFAULT);
- System.out.println(delete.isAcknowledged()); }
- @Test
- public void testIndexExists() throws Exception{
- GetIndexRequest getIndexRequest=new GetIndexRequest("qy151-index");
- boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
- System.out.println(exists);
- }
- //添加文档--PUT /索引/1 {name:"",age:"",address:""}
- @Test
- public void testInsertDoc() throws Exception{
- //ss-index为文档的名字
- IndexRequest indexRequest=new IndexRequest("ss-index");
- indexRequest.id("1");//指定文档的id
- //指定文档的内容:String文档的json内容,XContentType xContentType:以什么格式
- indexRequest.source(JSON.toJSONString(new User("张三","北京",22)), XContentType.JSON);
-
- IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
- System.out.println(indexResponse.getResult());
- }
- //获取文档 GET /索引/_doc/1
- @Test
- public void testGetDoc() throws Exception{
- GetRequest indexRequest=new GetRequest("ss-index");
- indexRequest.id("1");
- GetResponse getResponse = client.get(indexRequest, RequestOptions.DEFAULT);
- String string = getResponse.getSourceAsString();
- User user = JSON.parseObject(string, User.class);
-
- Map
map = getResponse.getSourceAsMap(); - System.out.println(map.get("address"));
- }
- //判断索引文档是否存在
- @Test
- public void testDocExist() throws Exception{
- GetRequest indexRequest=new GetRequest("ss-index");
- indexRequest.id("2");
- boolean exists = client.exists(indexRequest, RequestOptions.DEFAULT);
- System.out.println(exists);
- }
- //文档操作---删除文档
- @Test
- public void testDeleteDoc() throws Exception{
- DeleteRequest deleteRequest=new DeleteRequest("ss-index");
- deleteRequest.id("1");
- DeleteResponse delete = client.delete(deleteRequest,RequestOptions.DEFAULT);
-
- System.out.println(delete.getResult());
- }
- @Test
- public void testUpdateDoc()throws Exception{
- UpdateRequest updateRequest=new UpdateRequest("qy151-index","1");
- User user = new User();
- user.setName("刘德华");
- updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
- UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
- System.out.println(update.getResult());
- }
- //批量添加文档
- @Test
- public void testBuck() throws Exception{
- BulkRequest bulk=new BulkRequest("ss-index");
- List
list=new ArrayList<>(); - list.add(new User("2","张三","北京",22));
- list.add(new User("3","张三他爸","上海",22));
- list.add(new User("4","李四","杭州",22));
- list.add(new User("5","李四他妈","广州",22));
- list.add(new User("6","王五","南京",22));
-
- //list.stream().forEach(item->bulk.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
- for(User user:list){
- IndexRequest indexRequest=new IndexRequest();
- indexRequest.id(user.getId());
- indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
- bulk.add(indexRequest);
- }
-
-
-
- BulkResponse bulkResponse = client.bulk(bulk,RequestOptions.DEFAULT);
- System.out.println(bulkResponse.hasFailures());
- }
- //搜索查询---GET /索引/_search
- // {
- // "query":{
- // "":{}
- // },
- // "from":
- // "size":
- // "_source":["",""],
- // "sort":{}
-
- // }
-
- //1. 搜索请求对象SearchRequest
- //2. 构建一个条件对象SearchSourceBuilder
- //3. 把条件对象放入搜索请求对象中
- //4. 执行搜索功能
-
- @Test
- public void testSearch() throws Exception{
- //
- SearchRequest searchRequest=new SearchRequest("ss-index");
- //创建一个条件对象
- SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
- TermQueryBuilder matchQuery = QueryBuilders.termQuery("name", "张");
- sourceBuilder.query(matchQuery);
-
- //分页
- sourceBuilder.from(0);
- sourceBuilder.size(1);
-
- //排序
- // sourceBuilder.sort("age");
-
- //高亮
- HighlightBuilder highlightBuilder=new HighlightBuilder();
- highlightBuilder.field("name");
- highlightBuilder.preTags("");
- highlightBuilder.postTags("");
- sourceBuilder.highlighter(highlightBuilder);
-
- searchRequest.source(sourceBuilder);
-
-
- SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
-
- System.out.println("总条数:"+searchResponse.getHits().getTotalHits().value);
- SearchHit[] hits = searchResponse.getHits().getHits();
-
- Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
- Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
- }