import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class ElasticsearchServiceImpl {
private final RestHighLevelClient client;
@Autowired
public ElasticsearchServiceImpl(RestHighLevelClient client) {
this.client = client;
}
/**
* 获取分词词组
* @param text 被分词字符串
* @param analyzer 分词器
* @return
* @throws IOException
*/
public List<String> analyzeText(String text, String analyzer) throws IOException {
AnalyzeRequest analyzeRequest = AnalyzeRequest.withGlobalAnalyzer(analyzer, text);
AnalyzeResponse analyzeResponse = client.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
List<String> tokens = new ArrayList<>();
analyzeResponse.getTokens().forEach(token -> tokens.add(token.getTerm()));
return tokens;
}
}
实现分组
Map<Integer, List<KpiTabDetailVo>> detailMap = basicInfos.stream().collect(Collectors.groupingBy(KpiTabDetailVo::getBlockOrder))
实现字符串拼接
String deptIdStr = deptList.stream().collect(Collectors.joining(","));