Spring Date ElasticSearch

Spring Date ElasticSearch

  1. 导包
<?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>com.xcy</groupId>
    <artifactId>TestESABC</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--<properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--多导入一个包,否则报日志错误-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</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.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>
  1. 创建applicationContext,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: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">

    <elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch"
                                    cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>
    <elasticsearch:repositories base-package="com.xcy.repositories"/>

    <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="esClient"/>
    </bean>
</beans>

3.创建实体

package com.xcy.pojo;

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 = "es_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;

    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;
    }
}

4.创建接口

package com.xcy.repositories;

import com.xcy.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
}

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestES {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Test
    public void createIndex() {
        //创建索引库
        /*elasticsearchTemplate.createIndex(Article.class);*/
        //添加mappings映射
        elasticsearchTemplate.putMapping(Article.class);

    }

6.创建文档

@Test
    public void add() {
        Article article = new Article();
       //for (int i = 3; i < 100; i++) {
            article.setId(i);
            article.setTitle("老闫翻车合集" + i);
            articleRepository.save(article);
        //}
    }

7.删除文档

@Test
  public void delete() {

          articleRepository.deleteById(1l);
          //全部删除           
          //articleRepository.deleteAll();
  }

8.修改文档

和新增文档一样,Id相同的,先删除再新增。

还是add添加的方法。

9.普通查询

//查询所有文档
    @Test
    public void findDocumentAll() {
        Iterable<Article> iterable = articleRepository.findAll();
        iterable.forEach(article -> System.out.println(article));

    }
    //通过ID查询指定的文档
    @Test
    public void testFindById() {
        Optional<Article> optional = articleRepository.findById(45l);
        Article article = optional.get();
        System.out.println(article);

    }

常用查询命名规则

关键字命名规则解释示例
andfindByField1AndField2根据Field1和Field2获取数据findByTitleAndContent
orfindByField1OrField2根据Field1或Field2获取数据findByTitleOrContent
isfindByField根据Field获取数据findByTitle
notindByFieldNot根据Field获的补集数据findByTitleNot
betweenfindByFieldBetween根据Field获取指定范围数据findByPriceBetween
lessThanEqualfindByFieldLessThan获得小于等于指定范围的数据findByPriceLessThan

10.自定义查询

@Test
    public void findAll() {
        //"2"为title中的一个字
        /*
        还要在ArticleRepository接口中添加   List<Article>  findByTitle(String title);
        */
        //默认查询10条数据
        List<Article> list = articleRepository.findByTitle("2");

        list.stream().forEach(a -> System.out.println(a));
    }
/*@Test
publc void testFindByTitleOrConent(){
    articleRepositoty.findByTitleOrContent("mavan","商务")
        .forEach(a-> System.out.pringln(a));
        
}*/

11.带分页请况

@Test
    public void testFandByTitle() {
        Pageable pageable = PageRequest.of(0,20);
        List<Article> list = articleRepository.findByTitle("翻车",pageable);
        list.stream().forEach(article -> System.out.println(article));

以上查询方法是先分词,每个词之间是and的关系,一句话词语必须都包含才可以查询出来。

如果先让一句话进行分词查询,并且词语之间是or的关系,必须使用原生查询!!!!

原生查询只要包含分词就可以查到

@Test
    public void testNativeSearchQuery() {
        NativeSearchQuery query = new NativeSearchQueryBuilder()
            //如“翻车啦”在title中并没有啦这个字但含有翻车这个词就可以查询到
                .withQuery(QueryBuilders.queryStringQuery("翻车啦").defaultField("title"))
                .withPageable(PageRequest.of(0,15))
                .build();
        List<Article> list = elasticsearchTemplate.queryForList(query,Article.class);
        list.stream().forEach(article -> System.out.println(article));


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值