springboot——elasticsearch

一、安装

(1)elasticsearch:https://blog.csdn.net/zebra_916/article/details/78020630

(2)ik分词器:https://blog.csdn.net/shiyaru1314/article/details/54425828

注意:

1、不同版本服务器对应不同分词器,对照表:https://blog.csdn.net/shiyaru1314/article/details/54425828

2、安装分词器:从github上下载对应版本分词器-->解压-->到elasticsearuch 的plugin目录下-->创建ik文件夹-->把分词器解压后的复制到特定目录即可

3、经常报错及解决方式:https://blog.csdn.net/qq_35225231/article/details/78431664。答案在评论中

二、使用

(1)启动本地elasticsearch

(2)代码demo实现

1、依赖

<dependency><!-- 全文搜索引擎 -->
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>

2、配置

#全文搜索 elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300

3、dao配置

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.dal.entity.Article;

@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {
    List<Article> findByTitleContaining(String title);
}
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Repository;

import com.example.demo.dal.entity.Article;

@Repository
public class ArticleTemplate {
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

	public List<Article> queryByTitle(String title) {
		return elasticsearchTemplate.queryForList(new CriteriaQuery(Criteria.where("title").contains(title)),
				Article.class);
	}
}

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.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "cxytiandi02", type = "article")
public class Article {
	@Id
	@Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
	private Integer id;
	@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
	private String sid;
	@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
	private String title;
	@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
	private String url;
	@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
	private String content;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}

test

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.example.demo.dal.dao.ArticleRepository;
import com.example.demo.dal.dao.ArticleTemplate;
import com.example.demo.dal.entity.Article;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ArticleTest {
    @Autowired
    ArticleRepository articleRepository;
    @Autowired
    ArticleTemplate articleTemplate;
    @Test
    public void testAdd() {
        Article article = new Article();
        article.setId(2);
        article.setSid("dak219dksd");
        article.setTitle("java 好难啊");
        article.setUrl("http://baidu.com");
        article.setContent("java 及的垃圾的 的垃圾大家导入大大大");
        articleRepository.save(article);
    }
//    @Test
    public void testList() {
        Iterable<Article> list = articleRepository.findAll();
        for (Article article : list) {
            System.out.println(article.getTitle());
        }
    }
//    @Test
    public void testQuery() {
        Iterable<Article> list = articleRepository.findByTitleContaining("java");
        for (Article article : list) {
            System.out.println(article.getTitle());
        }
    }
    @Test
    public void testQueryByTitle() {
        List<Article> list = articleTemplate.queryByTitle("java");
        for (Article article : list) {
            System.out.println(article.getTitle());
        }
    }
}

效果结果:

猜你喜欢

转载自blog.csdn.net/qq_42683700/article/details/82745434
今日推荐