filterBuilders 构建过滤器query
FilterBuilders构建过滤器Query
package com.elasticsearch; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.*; /**
* Created by lw on 14-7-16.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* elasticsearch以提供了一个完整的Java查询dsl其余查询dsl。
* FilterBuilders工厂构建
* API:
* <a>http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/query-dsl-filters.html</a>
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public class Es_FilterBuilders_DSL { /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* and Filter
* 一个过滤器匹配文档匹配的布尔组合其他过滤器。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder andFilter() {
return FilterBuilders.andFilter(
FilterBuilders.rangeFilter("age").from(1).to(1000),
FilterBuilders.prefixFilter("name", "葫芦1493")
);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* bool Filter
* 一个过滤器匹配文档匹配的布尔组合其他过滤器。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder boolFilter() {
return FilterBuilders.boolFilter()
.must(FilterBuilders.termFilter("name", "葫芦1493娃"))
.mustNot(FilterBuilders.rangeFilter("age").from(1000).to(3000))
.should(FilterBuilders.termFilter("home", "山西省太原市7077街道"));
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* exists filter
* 一个过滤器来过滤字段唯一字段存在。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder existsFilter() {
return FilterBuilders.existsFilter("home");
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ids filter
* 创建一个新的id筛选提供 doc/映射类型。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder idsFilter() {
return FilterBuilders.idsFilter(Es_Utils.INDEX_DEMO_01_MAPPING, "type2")
.addIds("SNt0KdNbRdKmXJVaXfNxEA", "UDKtO4o9TgmDHIT4bk_OWw", "jkAZoHe9RWyjxyOnBCTdrw"); // Type is optional
//FilterBuilders.idsFilter().addIds("1", "4", "100");
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* limit filter
* 一个过滤器,用于限制结果提供的极限值(每个shard * 2 )。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder limitFilter() {
return FilterBuilders.limitFilter(2);//返回碎片shard*2 个结果
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* type filter
* 过滤type
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder typeFilter() {
return FilterBuilders.typeFilter(Es_Utils.INDEX_DEMO_01_MAPPING);
} /**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* geo bounding box filter
* 定义一个过滤器来过滤基于边界框左上角和右下角的位置/分
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder geoBoundingBoxFilter() {
return FilterBuilders.geoBoundingBoxFilter("pin.location")
.topLeft(40.73, -74.1)
.bottomRight(40.717, -73.99);
} /**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* geodistance filter
* 一个过滤器来过滤基于一个特定的距离从一个特定的地理位置/点。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder geoDistanceFilter() {
return FilterBuilders.geoDistanceFilter("pin.location")
.point(40, -70)
.distance(200, DistanceUnit.KILOMETERS)
.optimizeBbox("memory") // Can be also "indexed" or "none"
.geoDistance(GeoDistance.ARC); // Or GeoDistance.PLANE
} /**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* geo distance range filter
* 一个过滤器来过滤基于一个特定的范围从一个特定的地理位置/点。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder geoDistanceRangeFilter() {
return FilterBuilders.geoDistanceRangeFilter("pin.location")
.point(40, -70)
.from("200km")
.to("400km")
.includeLower(true)
.includeUpper(false)
.optimizeBbox("memory") // Can be also "indexed" or "none"
.geoDistance(GeoDistance.ARC); // Or GeoDistance.PLANE
} /**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* geo polygon filter
* 一个过滤器来过滤基于多边形定义为一组位置/分。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder geoPolygonFilter() {
return FilterBuilders.geoPolygonFilter("pin.location")
.addPoint(40, -70)
.addPoint(30, -80)
.addPoint(20, -90);
} /**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* has child / has parent filters
* 构造一个子过滤器,子类型和查询与文档、过滤的结果是父* *文档。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder hasChildFilter() {
// Has Child
FilterBuilders.hasChildFilter("blog_tag",
QueryBuilders.termQuery("tag", "something")); // Has Parent
return FilterBuilders.hasParentFilter("blog",
QueryBuilders.termQuery("tag", "something"));
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* match all filter
* 一个过滤器匹配所有文件。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder matchAllFilter() {
return FilterBuilders.matchAllFilter();
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* missing filter
* 一个过滤器来过滤字段唯一文件不存在。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder missingFilter() {
return FilterBuilders.missingFilter("name")
.existence(true)
.nullValue(true);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* not filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder notFilter() {
return FilterBuilders.notFilter(
FilterBuilders.rangeFilter("age").from(1000).to(2000));
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* or filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder orFilter() {
return FilterBuilders.orFilter(
FilterBuilders.termFilter("name", "葫芦1493娃"),
FilterBuilders.termFilter("name", "葫芦5083娃")
);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* prefix filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder prefixFilter() {
return FilterBuilders.prefixFilter("name", "葫芦5083");
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* query filter
* 一个过滤器,仅包装一个查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder queryFilter() {
return FilterBuilders.queryFilter(
QueryBuilders.queryString("name")
);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* range filter
* 过滤器,限制搜索结果值在给定的范围内。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder rangeFilter() {
FilterBuilders.rangeFilter("age")
.from(1000)
.to(2000)
.includeLower(true)
.includeUpper(false); // A simplified form using gte, gt, lt or lte
return FilterBuilders.rangeFilter("age")
.gte(1000)
.lt(2000);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* script filter
* 过滤器基于脚本的构建。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder scriptFilter() {
return FilterBuilders.scriptFilter(
"doc['age'].value > param1"
).addParam("param1", 1000);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* term filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder termFilter() {
return FilterBuilders.termFilter("name", "葫芦5083娃");
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* terms filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* The execution option now has the following options :
* <p/>
* plain
* The default. Works as today. Iterates over all the terms, building a bit set matching it, and filtering. The total filter is cached.
* <p/>
* fielddata
* Generates a terms filters that uses the fielddata cache to compare terms.
* This execution mode is great to use when filtering on a field that is already loaded into the fielddata cache from faceting, sorting, or index warmers.
* When filtering on a large number of terms, this execution can be considerably faster than the other modes.
* The total filter is not cached unless explicitly configured to do so.
* <p/>
* bool
* Generates a term filter (which is cached) for each term, and wraps those in a bool filter.
* The bool filter itself is not cached as it can operate very quickly on the cached term filters.
* <p/>
* and
* Generates a term filter (which is cached) for each term, and wraps those in an and filter.
* The and filter itself is not cached.
* <p/>
* or
* Generates a term filter (which is cached) for each term, and wraps those in an or filter.
* The or filter itself is not cached. Generally, the bool execution mode should be preferred.
*/
protected static FilterBuilder termsFilter() {
return FilterBuilders.termsFilter("name", "葫芦5083娃", "葫芦3582娃")
.execution("plain"); // 执行模式 Cane be either "plain", "bool" "and". Defaults to "plain".
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* nested filter
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder nestedFilter() {
return FilterBuilders.nestedFilter("obj1",
QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("obj1.name", "blue"))
.must(QueryBuilders.rangeQuery("obj1.count").gt(5))
);
} /**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* caching
* 缓存 过滤器
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static FilterBuilder cache() {
return FilterBuilders.andFilter(
FilterBuilders.rangeFilter("age").from(1000).to(9000),
FilterBuilders.prefixFilter("name", "葫芦3582")
)
.cache(true);//默认false
} public static void main(String[] args) {
Es_Utils.startupClient();
try {
searchTest(cache()); } catch (Exception e) {
e.printStackTrace();
} finally {
Es_Utils.shutDownClient();
}
} private static void searchTest(FilterBuilder filterBuilder) {
//预准备执行搜索
Es_Utils.client.prepareSearch(Es_Utils.INDEX_DEMO_01)
.setTypes(Es_Utils.INDEX_DEMO_01_MAPPING)
.setPostFilter(filterBuilder)
.setFrom(0).setSize(20).setExplain(true)
.execute()
//注册监听事件
.addListener(new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
Es_Utils.writeSearchResponse(searchResponse);
} @Override
public void onFailure(Throwable e) { }
});
}
}
filterBuilders 构建过滤器query的更多相关文章
- elasticsearch__5__java操作之FilterBuilders构建过滤器Query
FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...
- elasticsearch------java操作之QueryBuilders构建搜索Query
版权声明:本文为非原创文章,出处:http://blog.csdn.net/xiaohulunb/article/details/37877435. elasticsearch 分布式搜索系列专栏:h ...
- 【转载】Elasticsearch--java操作之QueryBuilders构建搜索Query
原文地址:https://www.cnblogs.com/pypua/articles/9459941.html package com.elasticsearch; import org.elast ...
- 核心J2EE模式 - 截取过滤器
核心J2EE模式 - 截取过滤器 背景 呈现层请求处理机制接收许多不同类型的请求,这些请求需要不同类型的处理.一些请求被简单转发到适当的处理程序组件,而其他请求必须在进一步处理之前进行修改,审核或未压 ...
- Elasticsearch 过滤器
序 本文主要记录es的查询过滤的使用. 使用过滤器 过滤器不影响评分,而评分计算让搜索变得复杂,而且需要CPU资源,因而尽量使用过滤器,而且过滤器容易被缓存,进一步提升查询的整体性能. post_fi ...
- 009-spring cloud gateway-过滤器GatewayFilter、GlobalFilter、GatewayFilterChain、作用、生命周期、GatewayFilterFactory内置过滤器
一.概述 在Spring-Cloud-Gateway之请求处理流程中最终网关是将请求交给过滤器链表进行处理. 核心接口:GatewayFilter,GlobalFilter,GatewayFilter ...
- elasticsearch 过滤器的种类
elasticsearch之查询过滤 elasticsearch elastic-search xixicat 2月13日发布 推荐 1 推荐 收藏 2 收藏,289 浏览 序 本文主要记录es的查询 ...
- Django(69)最好用的过滤器插件Django-filter
前言 如果需要满足前端各种筛选条件查询,我们使用drf自带的会比较麻烦,比如查询书名中包含"国"字,日期大于"2020-1-1"等等诸如此类的请求,Djan ...
- elasticsearch 通过HTTP RESTful API 操作数据
1.索引样例数据 下载样例数据集链接 下载后解压到ES的bin目录,然后加载到elasticsearch集群 curl -XPOST 127.0.0.1:9200/bank/account/_bulk ...
随机推荐
- Ubuntu下怎么退出vim编辑器
vim其实就是vi的升级版,vi里的所有命令vim里都可以用,一般使用来说几乎没什么差别. 写在前面:进入vim编辑器之后无法退出并保存,可以尝试给要编辑的文件sudo权限(例如:sudo vim / ...
- BCZM : 1.6
https://blog.csdn.net/kabini/article/details/2311946 题目大意: 水房能容纳饮料的总量是V,有一批饮料,每种饮料单个容量都是2的方幂,每种饮料信息如 ...
- String str = new String("abc"),这段代码一共生成了几个String对象?为什么?
String str = new String("abc")创建了俩个对象,首先为创建一个String对象"abc",然后在调用String类的构造方法时 pu ...
- Redis 常用的数据结构
String 字符串 set get 使用场景: 可以用来作为缓存使用(缓存更新策略和缓存雪崩如何处理) List lpop rpop lpush rpush 使用场景: set 无序集合 使用场景: ...
- thinkphp present标签
present标签用于判断某个变量是否已经定义,用法: 大理石平台精度等级 <present name="name"> name已经赋值 </present> ...
- thinkphp 输入变量
在Web开发过程中,我们经常需要获取系统变量或者用户提交的数据,这些变量数据错综复杂,而且一不小心就容易引起安全隐患,但是如果利用好ThinkPHP提供的变量获取功能,就可以轻松的获取和驾驭变量了. ...
- flask中abort()函数的使用
一.介绍 #从flask中导入abort from flask import abort abort()函数的作用 可以让开发者在检测到web访问错误时,立即将错误信息返回回去,返回的错误码必须是已知 ...
- 尚学python课程---13、python基础语法
尚学python课程---13.python基础语法 一.总结 一句话总结: legend2系统使我能够快速掌握一门语法,特别有用 pass 语句:空语句:是为了保持程序结构的完整性 :作用:比如: ...
- C++函数调用原理理解
空程序: int main() { 00411360 push ebp ;压入ebp 00411361 mov ebp,esp ;ebp = es ...
- System.Web.HttpCookie.cs
ylbtech-System.Web.HttpCookie.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken= ...