一.创建工程、导入坐标

1.选择Next

2.填写名称、选择位置、填写公司或组织、选择Finish

3.导入坐标

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.lb</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.0.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
</project>

4.创建实体类:Article

package cn.lb.es.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; @Document(indexName = "index_blog",type="article")
public class Article {
@Id
@Field(type= FieldType.Long,store = true)
private long id;
@Field(type =FieldType.text,store = true,analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String content; @Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
} public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}

5.创建接口

package cn.lb.es.repositories;

import cn.lb.es.entity.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.NoRepositoryBean; import java.util.List; public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
List<Article> findByTitleOrContent(String title, String content);
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}

6.添加配置

<?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
">
<!--elastic客户对象的配置-->
<elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302"/>
<!--配置包扫描器,扫描dao的接口-->
<elasticsearch:repositories base-package="cn.lb.es.repositories"/>
<!---->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean> </beans>

7.测试

import cn.lb.es.entity.Article;
import cn.lb.es.repositories.ArticleRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.web.PageableDefault;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest { @Autowired
private ArticleRepository articleRepositrory; @Autowired
private ElasticsearchTemplate template; @Test
public void createIndex() throws Exception{
template.createIndex(Article.class);
} @Test
public void addDocument() throws Exception
{ Article article =new Article();
article.setId(i);
article.setTitle("IntelliJ IDEA 编译Java程序出现 'Error:java: 无效的源发行版: 9' 的解决方案");
article.setContent("最新安装的IntelliJ IDEA 2018.1编译器,创建Java Project,并选择之前安装好的Eclipse配置的JDK,如图所示:");
articleRepositrory.save(article); } @Test
public void deleteDocumentId() throws Exception
{
articleRepositrory.deleteById(2l); //articleRepositrory.deleteAll();
} @Test
public void findByTitle() throws Exception
{
List<Article> list=articleRepositrory.findByTitle("9");
list.stream().forEach(a-> System.out.println(a));
} @Test
public void findByTitleOrContent() throws Exception
{
Pageable pageable=PageRequest.of(0,15);
List<Article> list = articleRepositrory.findByTitleOrContent("9", "n", pageable);
list.forEach(a-> System.out.println(a));
} @Test
public void nativeSearchQuery() throws Exception
{
NativeSearchQuery query=new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("Java是最好的语言").defaultField(
"title"
)).withPageable(PageRequest.of(0,15)).build(); List<Article> list = template.queryForList(query, Article.class);
list.forEach(a-> System.out.println(a));
}
}

ElasticSearch系列:基本操作(SpringDataElasticSearch)的更多相关文章

  1. Elasticsearch CRUD基本操作

    前言 本次我们聊一聊Elasticsearch的基本操作CRUD,他跟我们常用的关系型数据库的操作又有什么不一样的地方呢?今天我们就来好好讲解一番. 说明 本次演示用的版本是7.11. 工具可以使用K ...

  2. Elasticsearch rest-high-level-client 基本操作

    Elasticsearch rest-high-level-client 基本操作 本篇主要讲解一下 rest-high-level-client 去操作 Elasticsearch , 虽然这个客户 ...

  3. 5.ElasticSearch系列之文档的基本操作

    1. 文档写入 # create document. 自动生成 _id POST users/_doc { "user" : "shenjian", " ...

  4. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  5. Elasticsearch 系列文章汇总(持续更新...)

    系列文章列表 Query DSL Query DSL 概要,MatchAllQuery,全文查询简述 Match Query Match Phrase Query 和 Match Phrase Pre ...

  6. Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解

    Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...

  7. SpringBoot整合ElasticSearch:基于SpringDataElasticSearch

    0.注意事项 SpringDataElasticSearch可能和远程的ElasticSearch版本不匹配,会宝座 版本适配说明:https://github.com/spring-projects ...

  8. python对接elasticsearch的基本操作

    基本操作 #!/usr/bin/env python # -*- coding: utf-8 -*- # author tom from elasticsearch import Elasticsea ...

  9. Elasticsearch系列---实现分布式锁

    概要 Elasticsearch在文档更新时默认使用的是乐观锁方案,而Elasticsearch利用文档的一些create限制条件,也能达到悲观锁的效果,我们一起来看一看. 乐观锁与悲观锁 乐观锁 E ...

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

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

随机推荐

  1. threading 专递类对象

    import threading class MyClass: def __init__(self, name): self.name = name def my_method(self): prin ...

  2. .Net析构函数再论(CLR源码级的剖析)

    前言 碰到一些问题,发觉依旧没有全面了解完全析构函数.本篇继续看下析构函数的一些引申知识. 概述 析构函数目前发现的总共有三个标记,这里分别一一介绍下.先上一段代码: internal class P ...

  3. 4款.NET开源的Redis客户端驱动库

    前言 今天给大家推荐4款.NET开源免费的Redis客户端驱动库(以下排名不分先后). Redis是什么? Redis全称是REmote DIctionary Service,即远程字典服务.Redi ...

  4. CSS 还原拉斯维加斯球数字动画

    我的小册 <CSS 技术揭秘与实战通关>上线了,想了解更多有趣.进阶.系统化的 CSS 内容,可以猛击 - LINK. 最近大家刷抖音,是否有刷到拉斯维加斯的新地标 「Sphere」: 场 ...

  5. es6(1)

    1.var let const var---变量,重复定义不报错,没有块级作用域,不能限制修改 if(12>5){ var a=12; } alert(a); //弹出12 let---变量,重 ...

  6. 虹科分享 | 一起聊聊Redis企业版数据库与【微服务误解】那些事儿!

    如今,关于微服务依然存在许多误解,企业盲目追求这种炫酷技术并不可取.同时,这种盲目行为对于希望用微服务来有效解决问题的公司很不利.不是说任何特定的技术都是缺乏实际价值的,如微服务.Kubernetes ...

  7. Keepalived高可用软件概述

    Keepalived高可用软件概述: 1)互联网主要的高可用软件:Keepalived.Hearttbeat.其中Keepalived是轻量级的,Keepalived是一款开源.免费的实现网站.数据库 ...

  8. 一文读懂强化学习:RL全面解析与Pytorch实战

    在本篇文章中,我们全面而深入地探讨了强化学习(Reinforcement Learning)的基础概念.主流算法和实战步骤.从马尔可夫决策过程(MDP)到高级算法如PPO,文章旨在为读者提供一套全面的 ...

  9. upload—labs

    首先 常见黑名单绕过 $file_name = deldot($file_name);//删除文件名末尾的点上传 shell.php. $file_ext = strtolower($file_ext ...

  10. 线性代数导论MIT第二章知识点上

    线性代数导论MIT第二章求解线性方程组 2.1--2.2知识点 1.向量与线性方程组 2.不同角度看方程式 也就是矩阵的乘法原型: 以行来看方程式就是原式 以列来看方程式 以矩阵来看方程式 3.消元法 ...