1. 程式人生 > >springboot——elasticsearch

springboot——elasticsearch

一、安裝

注意:

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

效果結果: