es 启动需要分配超过2.6g的内存
默认端口9200,9300 ;
9200为http端口,9300为tcp端口
springboot-data-es 默认连接9300进行操作 存在问题:容易因为es版本不一致而无法启动
所以选择使用在项目中使用http访问
searchbox 发送http请求的client
elasticsearch 主要用于写es查询体
lucene-core es的核心包,当启动es后访问9200会出现该包的版本号
引入pom
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.6.1</version>
</dependency>
yml 配置
spring:
elasticsearch:
jest:
uris: http://localhost:9200
read-timeout: 20000 #读取超时
connection-timeout: 20000 #连接超时
具体使用
创建entity
public class EntityDo implements Serializable {
//库名
public static final String INDEX_NAME = "test";
//表名
public static final String TYPE = "entity";
private Integer id;
private String workerid;
private String content;
}
service
@Service
public class CaseService {
@Autowired
private JestClient jestClient;
//批量插入
public void saveEntity(List<EntityDo> EntityDos) {
Bulk.Builder bulk = new Bulk.Builder();
for(EntityDo entityDo: EntityDos) {
Index index = new Index.Builder(entityDo).index(EntityDo.INDEX_NAME).type(CaseDo.TYPE).build();
bulk.addAction(index);
}
try {
jestClient.execute(bulk.build());
} catch (IOException e) {
e.printStackTrace();
}
}
public List<String> searchFetch(String content){
//构造查询体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//匹配查询content 最小匹配度75%
searchSourceBuilder.query(QueryBuilders.matchQuery("content",content).minimumShouldMatch("75%"));
//聚合查询 将workerid作为主体 分组查询workerid出现最多的10条数据
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("workerid_count").field("workerid.keyword").size(10);
//将聚合查询加入查询体中
searchSourceBuilder.aggregation(aggregationBuilder);
//根据查询体,库名表名 创建查询
Search search = new Search.Builder(searchSourceBuilder.toString())
.addIndex(EntityDo.INDEX_NAME).addType(EntityDo.TYPE).build();
List<String> workids = new ArrayList<>();
try {
//发送请求
JestResult result = jestClient.execute(search);
//请求成功
if (result.isSucceeded()){
//从Agg中获取聚合查询中的结果
List<TermsAggregation.Entry> workerid_counts = ((SearchResult) result).getAggregations().getTermsAggregation("workerid_count").getBuckets();
for (TermsAggregation.Entry entry: workerid_counts
) {
workids.add(entry.getKeyAsString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return workids;
}
踩过的坑
关于hit与agg
es的查询,如果带有聚合查询就会返回带有agg的结果,通过遍历获取agg的内容即可获得聚合值
es的查询无论是普通的匹配查询还是聚合查询 都会带有hit值,hit表示所有满足查询条件的结果,里面不是聚合后的结果!!!!
之前不理解这2个关系,所以在java代码中聚合了hit以获得正确的解;但是通过验证agg中的值,结合es是用java写的,而且es的返回结果无论如何都带有hit数据,所以我认为我们发送的聚合查询的本质就是,es先查询出hit值,然后用java代码实现聚合后,将值加入agg后返回
网友评论