feat(手机端): 添加elasticsearch测试代码

master
hequan_waynaqua 4 years ago
parent a4ef1c4d56
commit e14b4eb0ae

@ -6,6 +6,10 @@ import com.wayn.common.base.ElasticEntity;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@ -27,6 +31,27 @@ public class ElasticController {
@Autowired
private IGoodsService iGoodsService;
@GetMapping("index")
public R index() {
String indexSql = "{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"isOnSale\": {\n" +
" \"type\": \"boolean\"\n" +
" }\n" +
" }\n" +
"}";
baseElasticService.createIndex("goods", indexSql);
return R.success();
}
@GetMapping("insert")
public R insert() {
List<Goods> list = iGoodsService.list();
@ -47,8 +72,27 @@ public class ElasticController {
@GetMapping("search")
public R search() {
// MultiSearchRequest request = new MultiSearchRequest();
// SearchRequest firstSearchRequest = new SearchRequest("goods");
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.matchQuery("name", "羊毛"));
// firstSearchRequest.source(searchSourceBuilder);
// request.add(firstSearchRequest);
// SearchRequest secondSearchRequest = new SearchRequest("goods");
// searchSourceBuilder = new SearchSourceBuilder();
//
// searchSourceBuilder.query(QueryBuilders.matchQuery("name", "手机"));
// secondSearchRequest.source(searchSourceBuilder);
// request.add(firstSearchRequest);
// request.add(secondSearchRequest);
// List<Object> list = baseElasticService.search(request, Object.class);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.matchQuery("name", "沃尔玛"));
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", "手机");
MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keyword", "手机");
boolQueryBuilder.should(matchQuery);
boolQueryBuilder.should(matchPhraseQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
List<Object> list = baseElasticService.search("goods", searchSourceBuilder, Object.class);
return R.success().add("data", list);
}

@ -65,6 +65,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/webjars/**").anonymous()
.antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous()
.antMatchers("/elastic/**").anonymous()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated().and()
.headers().frameOptions().disable();

@ -5,8 +5,11 @@ import com.wayn.common.base.ElasticEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
@ -20,6 +23,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -126,7 +130,7 @@ public class BaseElasticService {
list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
.source(item.getData(), XContentType.JSON)));
try {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -170,6 +174,26 @@ public class BaseElasticService {
}
}
public <T> List<T> search(MultiSearchRequest request, Class<T> c) {
try {
MultiSearchResponse response = restHighLevelClient.msearch(request, RequestOptions.DEFAULT);
MultiSearchResponse.Item[] responseResponses = response.getResponses();
List<T> all = new ArrayList<>();
for (MultiSearchResponse.Item item : responseResponses) {
SearchHits hits = item.getResponse().getHits();
List<T> res = new ArrayList<>();
for (SearchHit hit : hits) {
res.add(JSON.parseObject(hit.getSourceAsString(), c));
}
all.addAll(res);
}
return all;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* index
*

Loading…
Cancel
Save