QueryBuilder builder = null;
Map
if (map == null)
return null;
for (Map.Entry
if (entry.getValue() != null) {
builder = QueryBuilders.matchQuery(entry.getKey(),entry.getValue());
}
}
SearchResponse scrollResp =client.prepareSearch(indexs).setFrom(page.getPageNum() * page.getPageSize())
.highlighter(highlight).setSize(page.getPageSize())
.setQuery(builder).get();
List
for (SearchHit hit : scrollResp.getHits().getHits()) {
try {
Map
Article articleSearch =parseObject(param, hit.getSourceAsString());
String titleAdd = "";
for(Text textTemp :highlightResult.get("description").fragments()){
titleAdd += textTemp;
}
articleSearch.setTitle(titleAdd);
result.add(articleSearch);
} catch (Exception e) {
e.printStackTrace();
}
}
page.setTotal(scrollResp.getHits().totalHits);
page.setParam(param);
page.setRetList(result);
return page;
}
public static Map
List
@SuppressWarnings("rawtypes")
Class tempClass = o.getClass();
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass();
}
try {
Map
for (Field field : fieldList) {
if(field.isAnnotationPresent(ESearchTypeColumn.class)) {
PropertyDescriptordescriptor = new PropertyDescriptor(field.getName(), o.getClass());
result.put(field.getName(),descriptor.getReadMethod().invoke(o));
}
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 判断某个索引下type是否存在
*
* @param index
* @param type
* @return
*/
public boolean isTypeExist(String index, String type) {
returnclient.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists();
}
/**
* 判断索引是否存在
*
* @param index
* @return
*/
public boolean isIndexExist(String index) {
returnclient.admin().indices().prepareExists(index).execute().actionGet().isExists();
}
/**
* 创建type(存在则进行更新)
*
* @param index
* 索引名称
* @param type
* type名称
* @param o
* 要设置type的object
* @return
*/
public boolean createType(String index, String type, Object o) {
if (!isIndexExist(index)) {
log.error("{}索引不存在", index);
return false;
}
try {
// 若type存在则可通过该方法更新type
return client.admin().indices().preparePutMapping(index).setType(type).setSource(o).get().isAcknowledged();
} catch (Exception e) {
log.error("创建type失败,{}", e.getMessage());
e.printStackTrace();
return false;
}
}
public static XContentBuilder getXContentBuilderKeyValue(Object o) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
List
@SuppressWarnings("rawtypes")
Class tempClass = o.getClass();
while (tempClass != null) {// 当父类为null的时候说明到达了最上层的父类(Object类).
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass();// 得到父类,然后赋给自己
}
for (Field field : fieldList) {
if(field.isAnnotationPresent(ESearchTypeColumn.class)) {
PropertyDescriptordescriptor = new PropertyDescriptor(field.getName(), o.getClass());
Object value =descriptor.getReadMethod().invoke(o);
if (value != null) {
builder.field(field.getName(),value.toString());
}
}
}
builder.endObject();
log.debug(builder.string());
return builder;
} catch (Exception e) {
log.error("获取object key-value失败,{}", e.getMessage());
}
return null;
}
}
ESearchTypeColumn.java
package com.hc.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 构建为elasticsearch
* 方便使用的jsonBuilder对象
*@author huangcheng
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ESearchTypeColumn {
/**
* 字段类型
*
* @return
*/
String type() default "string";
/**
* 是否分词
*
* @return
*/
boolean analyze() default false;
}
Article.java
public classArticle{
publicstaticfinalString DEFAULT_TEMPLATE ="frontViewArticle";
privatestaticfinallong serialVersionUID= 1L;
privateCategory category;//分类编号
@ESearchTypeColumn
privateString title; // 标题
privateString link; // 外部链接
privateString color; // 标题颜色(red:红色;green:绿色;blue:蓝色;yellow:黄色;orange:橙色)
privateString image; // 文章图片
privateString keywords;//关键字
@ESearchTypeColumn
privateString description;//描述、摘要
ElasticSearchPage.java
package com.hc.common.persistence;
import java.util.List;
/**
* ES搜索引擎分页类
* @authorhuangcheng
* @param
*/
public classElasticSearchPage
privateString scrollId;
privatelongtotal;
privateintpageSize;
privateintpageNum;
privateT param;
privateList
privateList
publicList
returnscrollIds;
}
publicvoid setScrollIds(List
this.scrollIds =scrollIds;
}
publicList
returnretList;
}
publicvoid setRetList(List
this.retList =retList;
}
publicString getScrollId() {
returnscrollId;
}
publicvoid setScrollId(StringscrollId) {
this.scrollId =scrollId;
}
publiclong getTotal() {
returntotal;
}
publicvoid setTotal(longtotal){
this.total =total;
}
publicint getPageSize() {
if(pageSize > 50){
return50;
}else{
returnpageSize;
}
}
publicvoid setPageSize(intpageSize){
this.pageSize =pageSize;
}
publicint getPageNum() {
if(pageNum <=0){
return0;
}else{
returnpageNum -= 1;
}
}
publicvoid setPageNum(intpageNum){
this.pageNum =pageNum;
}
publicT getParam() {
returnparam;
}
publicvoid setParam(Tparam) {
this.param =param;
}
@Override
publicString toString() {
return"Page{" +
"scrollId='"+ scrollId + '\''+
",total=" + total +
",pageSize=" + pageSize +
",pageNum=" + pageNum +
",param=" + param +
",retList=" + retList +
",scrollIds=" + scrollIds +
'}';
}
}
Test
SearchBaseTest.java
packagecom.hc.search;
importorg.junit.runner.RunWith;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml"})
publicabstract class SearchBaseTest extends AbstractJUnit4SpringContextTests{
}
ArticleRepositoryTest.java
package com.hc.search;
import javax.annotation.Resource;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import com.hc.common.persistence.ElasticSearchPage;
import com.hc.common.persistence.TransportClientRepository;
import com.hc.modules.cms.entity.Article;
public classArticleRepositoryTest extendsSearchBaseTest{
@Resource(name="transportClientRepository")
TransportClientRepository client;
@Test
public voidfindByNameAndPrice() throwsException{
System.out.println(client.getIdx("blog1","type","2001"));
}
@Test
public voidsaveDoc() throws Exception{
Article article = new Article();
article.setDescription("东南太阳汽车出新车了DX9");
article.setTitle("旗帜迎风飘扬");
article.setId("2006");
article.setHits(5);
article.setBeginDateString("2017/6/7");
System.out.println(client.saveDoc("blog1","type",article.getId(),article));
}
@Test
public voidsearchFullText(){
Article param = new Article();
param.setDescription("太阳");
ElasticSearchPage
page.setPageSize(10);
HighlightBuilder highlight = new HighlightBuilder();
highlight.field("description").preTags("").postTags("");
page = client.searchFullText(param,page, highlight,"blog1");
for(Article aa : page.getRetList()){
System.out.println(aa.getId() +"===="+aa.getDescription()+"===title:=="+aa.getTitle());
}
System.out.println(page.getTotal());
}
}