全文索引擎 · Elasticsearch与Spring项目整合流程

什么是ElasticSearch?

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    应用场景:

 (查询的数据量非常庞大,涉及到的查询条件有 模糊查询)

   在一个系统中,一个查询条件去多张表中查询数据。用于全文搜索引擎

    基本安装与使用:

电脑环境已经具备JAVA环境下解压下载的软件进入/bin目录下双击elasticsearch.bat开始运行

JAVA环境安装这里不做详解,可上百度搜索安装

目录详解:

bin存放elasticsearch运行命令

config存放配置文件

lib存放elasticsearch运行依赖jar

modules存放elasticsearch模块

plugins存放插件

运行成功:

访问地址:http://127.0.0.1:9200


基本插件安装(head可视化界面):

**以head插件为例:

1、切换到elasticsearch 的运行命令目录,如:D:\elasticsearch-2.4.0\bin,执行如下命令:

  plugin.bat install mobz/elasticsearch-head

2、访问http://localhost:9200/_plugin/head/



*****项目整合思路*****

    框架整合:

    spring data elasticsearch
    
1、导入pom坐标
     elasticsearch     
    spring-data-elasticsearch

 
<dependency>
   <groupId>org.elasticsearch</groupId>
   <artifactId>elasticsearch</artifactId>
   <version>2.4.0</version>
</dependency>
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-elasticsearch</artifactId>
   <version>2.0.4.RELEASE</version>
</dependency>
   

2、配置web.xml

    这里与Spring整合不需要配置项目启动时的web.xml


3、配置applicationContext-elasticsearch.xml

    elasticsearch的使用需要启动它的 服务(启动之前已经安装好的服务器)

   
    1)配置elasticsearch的连接
    
<!-- 配置Client -->
<elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300"/>
    2)配置spring data elasticsearch的模板
 
    <!-- 配置搜索模板  -->
<bean id="elasticsearchTemplate" 
   class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
   <constructor-arg name="client" ref="client" />
</bean>
注意:模板的ID的名称不能随便写,只能为上面的ID名称    3)配置 dao的扫描,这里配置你需要的扫描的包名    
<elasticsearch:repositories base-package="cn.guyue.dao" />
4、测试项目的启动
    1)启动elasticsearch的服务器
    2)启动工程
    不报错,则表示框架整合基本成功!
   

*****spring data elasticsearch的使用*****

domain实体类(注解是重点)
    定义在实体类class上的
	@Document(indexName = "blog3", type = "article")
	@Document //定义文档属性
	// indexName 索引库的名称
	// type 文档类型

       
    定义在实体类属性上的
  
  @id    org.springframework.data.annotation.Id
  @Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
        

            index : 是否建索引

            FieldIndex.analyzed 建立索引

            FieldIndex.not_analyzed 不建立索引

            FieldIndex.no 不能被作为条件查询

        analyzer 指定的分词器

        store 是否存储

        searchAnalyzer 搜索条件指定的分词器

        type 字段数据类型

        (注意这里如果配置了分词器需要另外在服务器安装分词器插件,我这里使用了IK分词器)


dao层需要继承:
  className  extends ElasticsearchRepository

    最后一点和项目整合时最后启动项目,一定要把单独的配置文件导入到主要的配置文件里面。

否则无法加载

applicationContext.xml

<!-- 引入 elasticsearch 配置  -->
<import resource="applicationContext-elasticsearch.xml"/>

Elasticsearch.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/data/elasticsearch
      http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
   
   <!-- 搜索DAO 扫描 -->
   <elasticsearch:repositories base-package="cn.guyue.main.index" />
   
   <!-- 配置Client -->
   <elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300"/>
   
   <!-- 配置搜索模板  -->
   <bean id="elasticsearchTemplate" 
      class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
      <constructor-arg name="client" ref="client" />
   </bean>
</beans>

欢迎各位大牛提出问题奋斗

   

    
    
    
    
    
    









  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值