Elasticsearch常用函数

  • Post author:
  • Post category:其他




插入文档

IndexRequest request = new IndexRequest("索引").id("id值").source(Map数据);
RestHighLevelClient.index(request, RequestOptions.DEFAULT);



获取一个文档

GetRequest getRequest = new GetRequest("索引", "id值");
RestHighLevelClient.get(getRequest, RequestOptions.DEFAULT);



判断文档是否存在

GetRequest getRequest = new GetRequest("索引", "id值");
// 只是判断是否存在,禁止以下两个,可不管
getRequest.fetchSourceContext(new FetchSourceContext(false));	// 禁止获取_source
getRequest.storedFields("_none_");	// 禁止获取存储字段
RestHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);



删除文档

DeleteRequest deleteRequest = new DeleteRequest("索引", "id值");
client.delete(deleteRequest, RequestOptions.DEFAULT);



更新文档

UpdateRequest request = new UpdateRequest("索引", "id值").doc(Map数据);
RestHighLevelClient.update(request, RequestOptions.DEFAULT);



批量更新(批量操作)(增删改)

BulkRequest request = new BulkRequest();
request.add(new IndexRequest("索引").id("id值1").source(XContentType.JSON, "field", "foo", "user", "lucky"));
request.add(new IndexRequest("索引").id("id值2").source(XContentType.JSON, "field", "bar", "user", "Jon"));
request.add(new DeleteRequest("索引", "id值"));
request.add(new UpdateRequest("索引", "id值").doc(XContentType.JSON, "other", "test"));
RestHighLevelClient.bulk(request, RequestOptions.DEFAULT);



批量查询

MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item("索引", "id值1"));
request.add(new MultiGetRequest.Item("索引", "id值2"));
request.add(new MultiGetRequest.Item("索引", "id值3"));
RestHighLevelClient.mget(request, RequestOptions.DEFAULT);



异步操作

ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {
    @Override
    public void onResponse(SearchResponse searchResponse) {
        // 查询成功
    }

    @Override
    public void onFailure(Exception e) {
        // 查询失败
    }
};
RestHighLevelClient.indexAsync(request, RequestOptions.DEFAULT, listener);



条件构造查询

// 大致使用
SearchRequest request = new SearchRequest(索引);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.xxx());
request.source(searchSourceBuilder);
RestHighLevelClient.search(request, RequestOptions.DEFAULT, listener);

// 1.搜索请求(列举常用)
SearchRequest request = new SearchRequest(索引);	
request.types(类型);
request.types();
request.source(搜索源生成器);  //搜索源生成器 --> SearchSourceBuilder
request.source();

// 2.搜索源生成器(列举常用)
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(开始索引);		// LIMIT ?
searchSourceBuilder.size(查询数量);		// LIMIT x,?
searchSourceBuilder.query(查询生成器);	// 查询生成器 --> QueryBuilders
searchSourceBuilder.sort("key",SortOrder.排序规则);	// 多个排序就构建多个sort,按顺序执行
searchSourceBuilder.sort(SortBuilders.fieldSort("key").order(SortOrder.排序规则));

// 3.查询生成器(列举常用)
QueryBuilders;
// 3.1 复合过滤器(列举常用)
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(查询生成器);	// AND
boolQueryBuilder.should(查询生成器);	// OR
boolQueryBuilder.mustNot(查询生成器);// NOT
boolQueryBuilder.filter(查询生成器);
// 3.2 Ids查询器(列举常用)
IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery();
idsQueryBuilder.addIds(String ids...);
idsQueryBuilder.types(String types...);
// 3.3 范围查询器(列举常用)
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(key);
rangeQueryBuilder.gt(数值);	// 大于
rangeQueryBuilder.gte(数值);	// 大于等于
rangeQueryBuilder.lt(数值);	// 小于
rangeQueryBuilder.lte(数值);	// 小于等于
// 3.4 正则表达式查询器(列举常用)
RegexpQueryBuilder regexpQueryBuilder = QueryBuilders.regexpQuery(key,正则表达式);
// 3.5 模糊查询器(列举常用)	模糊查询,我们输入字符的大概,ES就可以去根据输入的内容大概去匹配一下结果
FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, 模糊值);
// 3.6 通配符查询器(列举常用)	*:通配符	?:占位符
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery(key,value);
// 3.7 前缀查询器(列举常用) 前缀查询,可以通过一个关键字去指定一个Field的前缀,从而查询到指定的文档
PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery(key,前缀值);
// 3.8 高层查询器(列举常用) 自动根据查询的字段类型采用不同的查询方式
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key,valye);
// 3.8.1 and或者or查询器(列举常用)
Operator operator = QueryBuilders.matchQuery(key,valye).operator();
// 3.9 查询所有(列举常用)
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
// 3.10 全值查询器(列举常用)全值匹配,搜索之前不会对搜索的关键字进行分词,直接去分词库中匹配
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key,value);
// 3.11 全值查询器(和termQuery差不多)(value是可变参数,value间是or连接,相当于in(value))(列举常用)
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(key,value1,value2...);



版权声明:本文为weixin_44151739原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。