jest操作 Elasticsearch
package com.lgmall.search;
import com.lgmall.search.esEntity.Article;
import com.lgmall.search.esEntity.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
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 java.io.IOException;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SearchApplicationTests {
@Autowired
private JestClient jestClient;
/**
* 利用jest来操作elasticSearch
*/
@Test
public void testEs() {
// 新增数据
Article article = new Article();
article.setAuthor("易中天");
article.setPrice(1000);
article.setPrice(1);
Index build = new Index.Builder(article).index("lg").type("article").build();
try {
jestClient.execute(build);
} catch (IOException e) {
e.printStackTrace();
}
}
// 利用jest来查询数据 (字符串拼接的查询参数)
@Test
public void getEsMessage() {
String searchStr = "{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"author\": \"易\"\n" +
" }\n" +
" }\n" +
"}";
// 如果查询字符串是空的话,则代表查询所有的数据,也可以指定查询字符串
Search search = new Search.Builder("").addIndex("lg").addType("article").build();
try {
SearchResult searchResult = jestClient.execute(search);
List<SearchResult.Hit<Article, Void>> hits = searchResult.getHits(Article.class);
for (SearchResult.Hit<Article, Void> hit : hits) {
Article article = hit.source;
System.out.println(article.getAuthor());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 利用java代码来封装查询参数
*/
@Test
public void getEsMessage02() throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// TODO bool start
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// 数组过滤
//ArrayList<Integer> ins = new ArrayList<>();
//ins.add(13);
//ins.add(40);
//TermsQueryBuilder score = new TermsQueryBuilder("score", ins);
// 单个查询
//TermQueryBuilder score = new TermQueryBuilder("score", 13);
//TermQueryBuilder score = new TermQueryBuilder("score", 13);
//boolQueryBuilder.filter(score);
//
//TermQueryBuilder score1 = new TermQueryBuilder("score", 40);
//boolQueryBuilder.filter(score1);
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name","我想查找海洋天堂或者是战狼电影");
//MatchAllQueryBuilder matchAllQueryBuilder = new MatchAllQueryBuilder(); //获取所有的数据
// must
boolQueryBuilder.must(matchQueryBuilder);
// TODO bool stop
// from
searchSourceBuilder.from(0);
// size
searchSourceBuilder.size(10);
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<p style='color=red'>");
highlightBuilder.postTags("</p>");
// highLight
searchSourceBuilder.highlighter(highlightBuilder);
// TODO 封装bool
searchSourceBuilder.query(boolQueryBuilder);
String queryString = searchSourceBuilder.toString();
System.out.println(queryString);
Search search = new Search.Builder(queryString).addIndex("move").addType("c_move").build();
try {
SearchResult result = jestClient.execute(search);
List<SearchResult.Hit<Movie, Void>> hits = result.getHits(Movie.class);
for (SearchResult.Hit<Movie, Void> hit : hits) {
Movie source = hit.source;
System.out.println(source.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
技术改变一切