配置文件
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring‐context.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="com.itheima.dao"/>
7)配置实体
基于spring data elasticsearch注解配置索引、映射和实体的关系
<!‐‐ 扫描Service包,创建Service的实体 ‐‐>
<context:component‐scan base‐package="com.itheima.service"/>
<!‐‐ 配置elasticSearch的连接 ‐‐>
<!‐‐ 配置elasticSearch的连接 ‐‐>
<elasticsearch:transport‐client id="client" cluster‐nodes="localhost:9300" cluster‐name="my‐elasticsearch"/>
<!‐‐ ElasticSearch模版对象 ‐‐>
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor‐arg name="client" ref="client"></constructor‐arg>
</bean>
</beans>
<elasticsearch:transport‐client id="client" cluster‐nodes="localhost:9300" cluster‐name="my‐elasticsearch"/>
一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能。一个集群由
一个唯一的名字标识,这个名字默认就是“elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集
群的名字,来加入这个集群
其中cluster-nodes为节点 name为节点名可以设置
单个节点的设置
#节点1的配置信息:
#集群名称,保证唯一
cluster.name: my‐elasticsearch
#节点名称,必须不一样
node.name: node‐1
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9200
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9300
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
一个搜索的映射对应一个实体类
//@Document 文档对象 (索引信息、文档类型 )
@Document(indexName="blog3",type="article")
public class Article {
//@Id 文档主键 唯一标识
@Id
//@Field 每个文档的字段配置(类型、是否分词、是否存储、分词器 )
@Field(store=true, index = false,type = FieldType.Integer)
private Integer id;
@Field(index=true,analyzer="ik_smart",store=true,searchAnalyzer="ik_smart",type =
FieldType.text)
private String title;
@Field(index=true,analyzer="ik_smart",store=true,searchAnalyzer="ik_smart",type =
FieldType.text)
private String content;
@Document(indexName=“blob3”,type=“article”):
indexName:索引的名称(必填项)
type:索引的类型
@Id:主键的唯一标识
@Field(index=true,analyzer=“ik_smart”,store=true,searchAnalyzer=“ik_smart”,type =
FieldType.text)
index:是否设置分词
analyzer:存储时使用的分词器
searchAnalyze:搜索时使用的分词器
store:是否存储
type: 数据类型
public class SpringDataESTest {
@Autowired
private ArticleService articleService;
@Autowired
private TransportClient client;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**创建索引和映射*/
@Test
public void createIndex(){
elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class);
}
/**测试保存文档*/
@Test
public void saveArticle(){
Article article = new Article();
article.setId(100);
article.setTitle("测试SpringData ElasticSearch");
article.setContent("Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操
作,将原始操作elasticSearch的客户端API 进行封装 \n" +
" Spring Data为Elasticsearch Elasticsearch项目提供集成搜索引擎");
articleService.save(article);
}
底层封装了crud的代码 直接调用就可以 因为dao的接口继承了框架里面的接口 实现类也由底层封装了