public class MethEsSerivceImpl implements MethEsSerivce {
@Autowired
private ElasticsearchClient client;
@Override
public boolean insertDoc(String indexName,EsBlog blog) throws IOException {
final IndexResponse response = client.index(builder -> builder.index(indexName).id(String.valueOf(blog.getId())).document(blog));
return response.version() > 0;
}
@Override
public List<Hit<EsBlog>> listByIndexName(String indexName) throws IOException {
//查询所有文档
// final SearchResponse<Object> response = client.search(builder -> builder.index(indexName), Object.class);
// final List<Hit<Object>> hits = response.hits().hits();
// return hits;
//matchAll
final SearchResponse<EsBlog> response = client.search(builder ->
builder.index(indexName)
.query(q ->
q.matchAll(
v->v
)), EsBlog.class);
System.err.println(response.hits().hits());
return response.hits().hits();
}
@Override
public void removeDocById(String indexName,Integer Id) throws IOException {
//根据ID删除文档
final DeleteResponse response = client.delete(builder -> builder.index(indexName).id(String.valueOf(Id)));
System.err.println(response.shards().successful());
}
@Override
public void updateDoc(String indexName, EsBlog blog) throws IOException {
//修改文档(覆盖)
final UpdateResponse<EsBlog> response = client.update(builder -> builder.index("produces").id(String.valueOf(blog.getId())).doc(blog), EsBlog.class);
System.err.println(response.shards().successful());
}
@Override
public boolean updateDocParm(String indexName, EsBlog blog) throws IOException {
//修改文档(部分修改)
// final Produce produce = new Produce("ccaabb123", "旺仔摇滚洞", "旺仔摇滚洞乱摇乱滚", 10.23D);
final UpdateResponse<EsBlog> response = client.update(builder -> builder.index("produces").id(String.valueOf(blog.getId()))
.doc(blog).docAsUpsert(true), EsBlog.class);
return response.version() > 0;
}
/**
* 高亮分词多字段查询
* @param indexName
* @param str
* @return
* @throws IOException
*/
@Override
public List<EsBlog> listByQuery(String indexName, String str) throws IOException {
//字段分词查询
//高亮map字段
Map<String, HighlightField> highlightFieldMap = new HashMap<>();
highlightFieldMap.put("title",new HighlightField.Builder().build());
highlightFieldMap.put("count",new HighlightField.Builder().build());
//查询
final SearchResponse<EsBlog> response = client.search(builder ->
builder.index(indexName)//索引名称
.query(q->q.queryString(qs->qs.fields(Arrays.asList("title","count")).query(str)))
.highlight(h -> h.preTags("<em>").postTags("</em>").fields(highlightFieldMap)),
EsBlog.class);
List<EsBlog> list = new ArrayList<>();
response.hits().hits().forEach(esBlogHit -> {
//高亮赋值
setHighlightField(esBlogHit.highlight(),esBlogHit.source());
assert esBlogHit.source() != null;
esBlogHit.source().setId(Long.valueOf(esBlogHit.id()));
list.add(esBlogHit.source());
});
return list;
}
public void setHighlightField(Map<String,List<String>> hightData,EsBlog esBlog){
if (hightData.get("title") != null)
esBlog.setTitle(hightData.get("title").get(0));
if (hightData.get("count") != null)
esBlog.setCount(hightData.get("count").get(0));
}
个人博客已经应用 详见个人页网址