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的更多相关文章

  1. elasticsearch__5__java操作之FilterBuilders构建过滤器Query

    FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...

  2. elasticsearch------java操作之QueryBuilders构建搜索Query

    版权声明:本文为非原创文章,出处:http://blog.csdn.net/xiaohulunb/article/details/37877435. elasticsearch 分布式搜索系列专栏:h ...

  3. 【转载】Elasticsearch--java操作之QueryBuilders构建搜索Query

    原文地址:https://www.cnblogs.com/pypua/articles/9459941.html package com.elasticsearch; import org.elast ...

  4. 核心J2EE模式 - 截取过滤器

    核心J2EE模式 - 截取过滤器 背景 呈现层请求处理机制接收许多不同类型的请求,这些请求需要不同类型的处理.一些请求被简单转发到适当的处理程序组件,而其他请求必须在进一步处理之前进行修改,审核或未压 ...

  5. Elasticsearch 过滤器

    序 本文主要记录es的查询过滤的使用. 使用过滤器 过滤器不影响评分,而评分计算让搜索变得复杂,而且需要CPU资源,因而尽量使用过滤器,而且过滤器容易被缓存,进一步提升查询的整体性能. post_fi ...

  6. 009-spring cloud gateway-过滤器GatewayFilter、GlobalFilter、GatewayFilterChain、作用、生命周期、GatewayFilterFactory内置过滤器

    一.概述 在Spring-Cloud-Gateway之请求处理流程中最终网关是将请求交给过滤器链表进行处理. 核心接口:GatewayFilter,GlobalFilter,GatewayFilter ...

  7. elasticsearch 过滤器的种类

    elasticsearch之查询过滤 elasticsearch elastic-search xixicat 2月13日发布 推荐 1 推荐 收藏 2 收藏,289 浏览 序 本文主要记录es的查询 ...

  8. Django(69)最好用的过滤器插件Django-filter

    前言   如果需要满足前端各种筛选条件查询,我们使用drf自带的会比较麻烦,比如查询书名中包含"国"字,日期大于"2020-1-1"等等诸如此类的请求,Djan ...

  9. elasticsearch 通过HTTP RESTful API 操作数据

    1.索引样例数据 下载样例数据集链接 下载后解压到ES的bin目录,然后加载到elasticsearch集群 curl -XPOST 127.0.0.1:9200/bank/account/_bulk ...

随机推荐

  1. .net 接受请求过来的流

    //接收POST过来的数据 System.IO.Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024 ...

  2. PHP之最长回文串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab"注意: " ...

  3. JQuery ajax提交表单及表单验证

    JQuery ajax提交表单及表单验证 博客分类: jsp/html/javascript/ajax/development Kit 开源项目   注:经过验证,formValidator只适合一个 ...

  4. mui--使用mui中的图文表格组件时出现一条横线的解决方法

    最近做的微信公众号点击链接跳转到H5页面,该H5页面使用mui做的,遇到的商品列表页出现一个横线的问题, 这个是修改前的图片 解决方法: <style type="text/css&q ...

  5. 8-5接口测试用例设计与编写2 rest-assured

    rest-assured 简约的接口测试DSL 支持xml json的结构化解析 支持xpath jsonpath gpath等多种解析方式 对Spring的支持比较前面 底层是httpclient ...

  6. windows安装cygwin实现gcc/g++/linux操作等功能

    首先安装cygwin:参照下列博客去cygwin官网下载即可.安装过程中记得勾选需要的安装包,比如gcc/gdb https://blog.csdn.net/qilvmilv/article/deta ...

  7. 模拟求root——cf1067B

    注意最后一轮要单独求一下 且最后只能有一个root #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 ...

  8. Vue-组件详解

    转载:https://segmentfault.com/a/1190000015199363 查看原文站点,更多扩展内容及更佳阅读体验! 组件详解 组件与复用 Vue组件需要注册后才可以使用.注册有全 ...

  9. Spring的refresh()方法相关异常

    如果是经常使用Spring,特别有自己新建ApplicationContext对象的经历的人,肯定见过这么几条异常消息:1.LifecycleProcessor not initialized - c ...

  10. Astyle 快速入门,常用指令

    --style=java -n -p -c !E astyle是一个命令行工具,命令语法很简单:          astyle [options] < original > Beauti ...