ElasticsearchDemo

DEMO使用技术点:Maven+ SpringBoot + JDK 1.8.0_151 + ElasticSearch-2.3.3 + ElasticSearch-analysis-ik-1.9.3 + typeahead.js输入框输入信息后,自动提示补全控件 + jqPaginator.js分页控件+ Thymeleaf 页面模板显示 

Demo Github 地址:https://github.com/SUNNINIU/ElasticsearchDemo

项目效果图:



ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
我们建立一个网站或应用程序,并要添加搜索功能,但是想要完成搜索工作的创建是非常困难的。我们希望搜索解决方案要运行速度快,我们希望能有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP来索引数据,我们希望我们的搜索服务器始终可用,我们希望能够从一台开始并扩展到数百台,
我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。因此我们利用Elasticsearch来解决所有这些问题以及可能出现的更多其它问题。

ElasticSearch 从2.x 至5.x api改动较大,本文仅以elasticsearch-2.3.3作为Demo,其他version操作详细查看官网 https://www.elastic.co/products/elasticsearch

一. Demo 技术列表:

  01.SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置,详细请看http://projects.spring.io/spring-boot/


  02.ElasticSearch-2.3.3  ElasticSearch是一个基于Lucene的搜索服务器 详细看官网 https://www.elastic.co/products/elasticsearch


  03.ElasticSearch-analysis-ik-1.9.3 是ElasticSearch的一个分词插件,elasticsearch-analysis-ik 是一款中文的分词插件,支持自定义词库,详细请看https://github.com/medcl/elasticsearch-analysis-ik


  04.Typeahead.js 输入框输入信息后,自动提示补全控件,详细请看 https://github.com/twitter/typeahead.js/


  05.jqPaginator.js分页控件,详细请看作者网址:http://jqpaginator.keenwon.com/


  06.Thymeleaf 页面模板显示,目前Spring官方已经不推荐使用JSP来开发WEB了,推荐使用Thymeleaf作为前端页面 详细请看官网:http://www.thymeleaf.org/


Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>elasticsearch</groupId>
<artifactId>ElasticsearchDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ElasticsearchDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
  <!-- spring boot 基本环境 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<!--es2.3.3  -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.6</version>
</dependency>
    <!--springboot  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--MySql  -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--thymeleaf  -->
<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-thymeleaf</artifactId>  
</dependency> 
</dependencies>
<build>
<finalName>ElasticsearchDemo</finalName>
</build>
</project>

二、DEMO DOC

1.本文以window elasticsearch-2.3.3 version为例, 下载地址 https://www.elastic.co/downloads/past-releases 选择相应version 下载.zip ;

2.解压.zip , 目录elasticsearch-2.3.3\bin 双击 elasticsearch.bat 启动elasticsearch;

3.安装 ik 分词 

  01.ik下载地址:https://github.com/medcl/elasticsearch-analysis-ik


   02.在github 上有相应的ik--> elasticsearch 版本对应表,请按照对应表下载对应 version 的ik.zip; 本文使用elasticsearch-2.3.3 对应ik版本为 elasticsearch-analysis-ik-1.9.3。


   03.下载 elasticsearch-analysis-ik-1.9.3.zip ,解压后使用dos 命令进入到解压文件夹的根目录,使用 mvn clean package  命令对ik 进行打包。命令执行完毕创建elasticsearch-analysis-ik-1.9.3-sources.jar,elasticsearch-analysis-ik-1.9.3.jar 以及一些文件在target文件夹下表示打包完毕。


 04.在elasticsearch-2.3.3中手动创建目录plugins\analysis-ik ,将 elasticsearch-analysis-ik-1.9.3\target\releases\elasticsearch-analysis-ik-1.9.3 目录下的plugin-descriptor.properties及整个文件夹config, 和elasticsearch-analysis-ik-1.9.3.jar 拷贝到该目录下。


 05.将 elasticsearch-analysis-ik-1.9.3\target\releases\elasticsearch-analysis-ik-1.9.3 所有.jar 拷贝到 elasticsearch-2.3.3 根目录lib下。


  06.在elasticsearch-2.3.3的配置文件config/elasticsearch.yml中最后增加ik的配置


  index:  
 analysis:                     
analyzer:        
 ik:  
 alias: [ik_analyzer]  
 type: org.elasticsearch.index.analysis.IkAnalyzerProvider  
 ik_max_word:  
 type: ik  
 use_smart: false  
 ik_smart:  
 type: ik  
 use_smart: true 
   或者

  index.analysis.analyzer.ik.type : “ik”  

  07.重启elasticsearch-2.3.3,正常启动表示配置成功。

4.使用maven 导入demo


5.执行package com.net.Application.java 类启动SpringBoot, 启动会执行 package com.net.InitESData.java 初始化测试数据


6.成功启动完毕后,在浏览器上输入 http://localhost:8080/index 便可进行预览测试。


Github地址:https://github.com/SUNNINIU/ElasticsearchDemo

备注:任何问题在github提交Issues,有时间会及时修复提交代码。
本demo 参考文章 http://blog.csdn.net/hunanlzg/article/details/51658370 感谢!
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,以下是一个简单的 Java 连接 Elasticsearch 并进行增删改查的示例代码: ```java import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.sort.SortOrder; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class ElasticsearchDemo { private RestHighLevelClient client; public ElasticsearchDemo() { // TODO 连接 Elasticsearch 的代码 } // 新增文档 public void addDocument(String index, String type, String id, Map<String, Object> source) throws IOException { IndexRequest request = new IndexRequest(index, type, id) .source(source); IndexResponse response = client.index(request); System.out.println(response); } // 新增文档(使用 XContentBuilder) public void addDocument(String index, String type, String id, XContentBuilder builder) throws IOException { IndexRequest request = new IndexRequest(index, type, id) .source(builder); IndexResponse response = client.index(request); System.out.println(response); } // 修改文档 public void updateDocument(String index, String type, String id, Map<String, Object> source) throws IOException { UpdateRequest request = new UpdateRequest(index, type, id) .doc(source); UpdateResponse response = client.update(request); System.out.println(response); } // 修改文档(使用 XContentBuilder) public void updateDocument(String index, String type, String id, XContentBuilder builder) throws IOException { UpdateRequest request = new UpdateRequest(index, type, id) .doc(builder); UpdateResponse response = client.update(request); System.out.println(response); } // 删除文档 public void deleteDocument(String index, String type, String id) throws IOException { DeleteRequest request = new DeleteRequest(index, type, id); DeleteResponse response = client.delete(request); System.out.println(response); } // 查询文档 public void getDocument(String index, String type, String id) throws IOException { GetRequest request = new GetRequest(index, type, id); GetResponse response = client.get(request); System.out.println(response); } // 复合查询 public void searchDocuments(String index) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchQuery("name", "john")) .from(0) .size(5) .sort("age", SortOrder.DESC) .fetchSource(new String[] {"name", "age"}, null); SearchResponse response = client.search(new SearchRequest(index) .source(builder)); System.out.println(response); } public static void main(String[] args) throws IOException { ElasticsearchDemo demo = new ElasticsearchDemo(); Map<String, Object> source = new HashMap<>(); source.put("name", "john"); source.put("age", 20); demo.addDocument("test", "user", "1", source); demo.getDocument("test", "user", "1"); demo.updateDocument("test", "user", "1", XContentFactory.jsonBuilder() .startObject() .field("name", "tom") .endObject()); demo.deleteDocument("test", "user", "1"); demo.searchDocuments("test"); } } ``` 请注意,这只是一个简单的示例,实际使用时需要根据具体场景进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值