- 浏览: 377211 次
- 性别:
- 来自: 四川
文章分类
- 全部博客 (247)
- 数据库以及sql (11)
- java (48)
- 爬虫学习 (20)
- java编程 (28)
- python编程以及安装和配置 (27)
- oracle数据库 (32)
- mongodb基本操作 (4)
- linux学习 (7)
- easyUI (2)
- nodeJs (8)
- python学习 (16)
- 其他 (13)
- hadoop (1)
- svn (1)
- 汉字 (1)
- windows (2)
- jsp (5)
- fiddler (1)
- ETL (1)
- teamviewer (1)
- maven (4)
- GIT (2)
- datagrip (1)
- ocr (1)
- redis (3)
- mysql (3)
- linux (1)
- 数据服务 (0)
最新评论
来源:http://blog.csdn.net/xiaohulunb/article/details/37877435
package com.elasticsearch;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.IndicesQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
/**
* Created by lw on 14-7-15.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* elasticsearch以提供了一个完整的Java查询dsl其余查询dsl。
* QueryBuilders工厂构建
* API:
* <a>http://www.elasticsearch.org/gui ... ries.html</a>
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public class Es_QueryBuilders_DSL {
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* match query 单个匹配
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder matchQuery() {
return QueryBuilders.matchQuery("name", "葫芦4032娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* multimatch query
* 创建一个匹配查询的布尔型提供字段名称和文本。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder multiMatchQuery() {
//现住址和家乡在【山西省太原市7429街道】的人
return QueryBuilders.multiMatchQuery(
"山西省太原市7429街道", // Text you are looking for
"home", "now_home" // Fields you query on
);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* boolean query and 条件组合查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder booleanQuery() {
return QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("name", "葫芦3033娃"))
.must(QueryBuilders.termQuery("home", "山西省太原市7967街道"))
.mustNot(QueryBuilders.termQuery("isRealMen", false))
.should(QueryBuilders.termQuery("now_home", "山西省太原市"));
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ids query
* 构造一个只会匹配的特定数据 id 的查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder idsQuery() {
return QueryBuilders.idsQuery().ids("CHszwWRURyK08j01p0Mmug", "ojGrYKMEQCCPvh75lHJm3A");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* constant score query
* 另一个查询和查询,包裹查询只返回一个常数分数等于提高每个文档的查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder constantScoreQuery() {
/*return // Using with Filters
QueryBuilders.constantScoreQuery(FilterBuilders.termFilter("name", "kimchy"))
.boost(2.0f);*/
// With Queries
return QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "葫芦3033娃"))
.boost(2.0f);
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* disjunction max query
* 一个生成的子查询文件产生的联合查询,
* 而且每个分数的文件具有最高得分文件的任何子查询产生的,
* 再加上打破平手的增加任何额外的匹配的子查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder disMaxQuery() {
return QueryBuilders.disMaxQuery()
.add(QueryBuilders.termQuery("name", "kimchy")) // Your queries
.add(QueryBuilders.termQuery("name", "elasticsearch")) // Your queries
.boost(1.2f)
.tieBreaker(0.7f);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* fuzzy query
* 使用模糊查询匹配文档查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder fuzzyQuery() {
return QueryBuilders.fuzzyQuery("name", "葫芦3582");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* has child / has parent
* 父或者子的文档查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder hasChildQuery() {
return // Has Child
QueryBuilders.hasChildQuery("blog_tag",
QueryBuilders.termQuery("tag", "something"));
// Has Parent
/*return QueryBuilders.hasParentQuery("blog",
QueryBuilders.termQuery("tag","something"));*/
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* matchall query
* 查询匹配所有文件。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder matchAllQuery() {
return QueryBuilders.matchAllQuery();
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* more like this (field) query (mlt and mlt_field)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder moreLikeThisQuery() {
// mlt Query
QueryBuilders.moreLikeThisQuery("home", "now_home") // Fields
.likeText("山西省太原市7429街道") // Text
.minTermFreq(1) // Ignore Threshold
.maxQueryTerms(12); // Max num of Terms
// in generated queries
// mlt_field Query
return QueryBuilders.moreLikeThisFieldQuery("home") // Only on single field
.likeText("山西省太原市7429街道")
.minTermFreq(1)
.maxQueryTerms(12);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* prefix query
* 包含与查询相匹配的文档指定的前缀。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder prefixQuery() {
return QueryBuilders.prefixQuery("name", "葫芦31");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* querystring query
* 查询解析查询字符串,并运行它。有两种模式,这种经营。
* 第一,当没有添加字段(使用{ @link QueryStringQueryBuilder #字段(String)},将运行查询一次,非字段前缀
* 将使用{ @link QueryStringQueryBuilder # defaultField(字符串)}。
* 第二,当一个或多个字段
* (使用{ @link QueryStringQueryBuilder #字段(字符串)}),将运行提供的解析查询字段,并结合
* 他们使用DisMax或者一个普通的布尔查询(参见{ @link QueryStringQueryBuilder # useDisMax(布尔)})。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder queryString() {
return QueryBuilders.queryString("+kimchy -elasticsearch");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* range query
* 查询相匹配的文档在一个范围。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder rangeQuery() {
return QueryBuilders
.rangeQuery("name")
.from("葫芦1000娃")
.to("葫芦3000娃")
.includeLower(true) //包括下界
.includeUpper(false); //包括上界
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* span queries (first, near, not, or, term)
* 跨度查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder spanQueries() {
// Span First
QueryBuilders.spanFirstQuery(
QueryBuilders.spanTermQuery("name", "葫芦580娃"), // Query
30000 // Max查询范围的结束位置
);
// Span Near TODO NotSolved
QueryBuilders.spanNearQuery()
.clause(QueryBuilders.spanTermQuery("name", "葫芦580娃")) // Span Term Queries
.clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"))
.slop(30000) // Slop factor
.inOrder(false)
.collectPayloads(false);
// Span Not TODO NotSolved
QueryBuilders.spanNotQuery()
.include(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
.exclude(QueryBuilders.spanTermQuery("home", "山西省太原市2552街道"));
// Span Or TODO NotSolved
return QueryBuilders.spanOrQuery()
.clause(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"));
// Span Term
//return QueryBuilders.spanTermQuery("name", "葫芦580娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* term query
* 一个查询相匹配的文件包含一个术语。。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder termQuery() {
return QueryBuilders.termQuery("name", "葫芦580娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* terms query
* 一个查询相匹配的多个value
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder termsQuery() {
return QueryBuilders.termsQuery("name", // field
"葫芦580娃", "葫芦3812娃") // values
.minimumMatch(1); // 设置最小数量的匹配提供了条件。默认为1。
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* top children query
* 构建了一种新的评分的子查询,与子类型和运行在子文档查询。这个查询的结果是,那些子父文档文件匹配。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder topChildrenQuery() {
return QueryBuilders.topChildrenQuery(
"blog_tag", // field
QueryBuilders.termQuery("name", "葫芦3812娃") // Query
)
.score("max") // max, sum or avg
.factor(5)
.incrementalFactor(2);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* wildcard query
* 实现了通配符搜索查询。支持通配符* < /tt>,<tt>
* 匹配任何字符序列(包括空),<tt> ? < /tt>,
* 匹配任何单个的字符。注意该查询可以缓慢,因为它
* 许多方面需要遍历。为了防止WildcardQueries极其缓慢。
* 一个通配符词不应该从一个通配符* < /tt>或<tt>
* < /tt> <tt> ?。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder wildcardQuery() {
return QueryBuilders.wildcardQuery("name", "葫芦*2娃");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* nested query
* 嵌套查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder nestedQuery() {
return QueryBuilders.nestedQuery("location", // Path
QueryBuilders.boolQuery() // Your query
.must(QueryBuilders.matchQuery("location.lat", 0.962590433140581))
.must(QueryBuilders.rangeQuery("location.lon").lt(0.00000000000000000003))
)
.scoreMode("total"); // max, total, avg or none
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* indices query
* 索引查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static IndicesQueryBuilder indicesQuery() {
// Using another query when no match for the main one
QueryBuilders.indicesQuery(
QueryBuilders.termQuery("name", "葫芦3812娃"),
Es_Utils.INDEX_DEMO_01, "index2"
) //设置查询索引上执行时使用不匹配指数
.noMatchQuery(QueryBuilders.termQuery("age", "葫芦3812娃"));
// Using all (match all) or none (match no documents)
return QueryBuilders.indicesQuery(
QueryBuilders.termQuery("name", "葫芦3812娃"),
Es_Utils.INDEX_DEMO_01, "index2"
) // 设置不匹配查询,可以是 all 或者 none
.noMatchQuery("none");
}
public static void main(String[] args) {
Es_Utils.startupClient();
try {
searchTest(indicesQuery());
} catch (Exception e) {
e.printStackTrace();
} finally {
Es_Utils.shutDownClient();
}
}
private static void searchTest(QueryBuilder queryBuilder) {
//预准备执行搜索
Es_Utils.client.prepareSearch(Es_Utils.INDEX_DEMO_01)
.setTypes(Es_Utils.INDEX_DEMO_01_MAPPING)
.setQuery(queryBuilder)
.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) {
}
});
}
}
package com.elasticsearch;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.IndicesQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
/**
* Created by lw on 14-7-15.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* elasticsearch以提供了一个完整的Java查询dsl其余查询dsl。
* QueryBuilders工厂构建
* API:
* <a>http://www.elasticsearch.org/gui ... ries.html</a>
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public class Es_QueryBuilders_DSL {
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* match query 单个匹配
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder matchQuery() {
return QueryBuilders.matchQuery("name", "葫芦4032娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* multimatch query
* 创建一个匹配查询的布尔型提供字段名称和文本。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder multiMatchQuery() {
//现住址和家乡在【山西省太原市7429街道】的人
return QueryBuilders.multiMatchQuery(
"山西省太原市7429街道", // Text you are looking for
"home", "now_home" // Fields you query on
);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* boolean query and 条件组合查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder booleanQuery() {
return QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("name", "葫芦3033娃"))
.must(QueryBuilders.termQuery("home", "山西省太原市7967街道"))
.mustNot(QueryBuilders.termQuery("isRealMen", false))
.should(QueryBuilders.termQuery("now_home", "山西省太原市"));
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ids query
* 构造一个只会匹配的特定数据 id 的查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder idsQuery() {
return QueryBuilders.idsQuery().ids("CHszwWRURyK08j01p0Mmug", "ojGrYKMEQCCPvh75lHJm3A");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* constant score query
* 另一个查询和查询,包裹查询只返回一个常数分数等于提高每个文档的查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder constantScoreQuery() {
/*return // Using with Filters
QueryBuilders.constantScoreQuery(FilterBuilders.termFilter("name", "kimchy"))
.boost(2.0f);*/
// With Queries
return QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "葫芦3033娃"))
.boost(2.0f);
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* disjunction max query
* 一个生成的子查询文件产生的联合查询,
* 而且每个分数的文件具有最高得分文件的任何子查询产生的,
* 再加上打破平手的增加任何额外的匹配的子查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder disMaxQuery() {
return QueryBuilders.disMaxQuery()
.add(QueryBuilders.termQuery("name", "kimchy")) // Your queries
.add(QueryBuilders.termQuery("name", "elasticsearch")) // Your queries
.boost(1.2f)
.tieBreaker(0.7f);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* fuzzy query
* 使用模糊查询匹配文档查询。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder fuzzyQuery() {
return QueryBuilders.fuzzyQuery("name", "葫芦3582");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* has child / has parent
* 父或者子的文档查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder hasChildQuery() {
return // Has Child
QueryBuilders.hasChildQuery("blog_tag",
QueryBuilders.termQuery("tag", "something"));
// Has Parent
/*return QueryBuilders.hasParentQuery("blog",
QueryBuilders.termQuery("tag","something"));*/
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* matchall query
* 查询匹配所有文件。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder matchAllQuery() {
return QueryBuilders.matchAllQuery();
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* more like this (field) query (mlt and mlt_field)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder moreLikeThisQuery() {
// mlt Query
QueryBuilders.moreLikeThisQuery("home", "now_home") // Fields
.likeText("山西省太原市7429街道") // Text
.minTermFreq(1) // Ignore Threshold
.maxQueryTerms(12); // Max num of Terms
// in generated queries
// mlt_field Query
return QueryBuilders.moreLikeThisFieldQuery("home") // Only on single field
.likeText("山西省太原市7429街道")
.minTermFreq(1)
.maxQueryTerms(12);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* prefix query
* 包含与查询相匹配的文档指定的前缀。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder prefixQuery() {
return QueryBuilders.prefixQuery("name", "葫芦31");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* querystring query
* 查询解析查询字符串,并运行它。有两种模式,这种经营。
* 第一,当没有添加字段(使用{ @link QueryStringQueryBuilder #字段(String)},将运行查询一次,非字段前缀
* 将使用{ @link QueryStringQueryBuilder # defaultField(字符串)}。
* 第二,当一个或多个字段
* (使用{ @link QueryStringQueryBuilder #字段(字符串)}),将运行提供的解析查询字段,并结合
* 他们使用DisMax或者一个普通的布尔查询(参见{ @link QueryStringQueryBuilder # useDisMax(布尔)})。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder queryString() {
return QueryBuilders.queryString("+kimchy -elasticsearch");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* range query
* 查询相匹配的文档在一个范围。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder rangeQuery() {
return QueryBuilders
.rangeQuery("name")
.from("葫芦1000娃")
.to("葫芦3000娃")
.includeLower(true) //包括下界
.includeUpper(false); //包括上界
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* span queries (first, near, not, or, term)
* 跨度查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder spanQueries() {
// Span First
QueryBuilders.spanFirstQuery(
QueryBuilders.spanTermQuery("name", "葫芦580娃"), // Query
30000 // Max查询范围的结束位置
);
// Span Near TODO NotSolved
QueryBuilders.spanNearQuery()
.clause(QueryBuilders.spanTermQuery("name", "葫芦580娃")) // Span Term Queries
.clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"))
.slop(30000) // Slop factor
.inOrder(false)
.collectPayloads(false);
// Span Not TODO NotSolved
QueryBuilders.spanNotQuery()
.include(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
.exclude(QueryBuilders.spanTermQuery("home", "山西省太原市2552街道"));
// Span Or TODO NotSolved
return QueryBuilders.spanOrQuery()
.clause(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
.clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"));
// Span Term
//return QueryBuilders.spanTermQuery("name", "葫芦580娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* term query
* 一个查询相匹配的文件包含一个术语。。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder termQuery() {
return QueryBuilders.termQuery("name", "葫芦580娃");
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* terms query
* 一个查询相匹配的多个value
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder termsQuery() {
return QueryBuilders.termsQuery("name", // field
"葫芦580娃", "葫芦3812娃") // values
.minimumMatch(1); // 设置最小数量的匹配提供了条件。默认为1。
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* top children query
* 构建了一种新的评分的子查询,与子类型和运行在子文档查询。这个查询的结果是,那些子父文档文件匹配。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder topChildrenQuery() {
return QueryBuilders.topChildrenQuery(
"blog_tag", // field
QueryBuilders.termQuery("name", "葫芦3812娃") // Query
)
.score("max") // max, sum or avg
.factor(5)
.incrementalFactor(2);
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* wildcard query
* 实现了通配符搜索查询。支持通配符* < /tt>,<tt>
* 匹配任何字符序列(包括空),<tt> ? < /tt>,
* 匹配任何单个的字符。注意该查询可以缓慢,因为它
* 许多方面需要遍历。为了防止WildcardQueries极其缓慢。
* 一个通配符词不应该从一个通配符* < /tt>或<tt>
* < /tt> <tt> ?。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder wildcardQuery() {
return QueryBuilders.wildcardQuery("name", "葫芦*2娃");
}
/**
* TODO NotSolved
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* nested query
* 嵌套查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static QueryBuilder nestedQuery() {
return QueryBuilders.nestedQuery("location", // Path
QueryBuilders.boolQuery() // Your query
.must(QueryBuilders.matchQuery("location.lat", 0.962590433140581))
.must(QueryBuilders.rangeQuery("location.lon").lt(0.00000000000000000003))
)
.scoreMode("total"); // max, total, avg or none
}
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* indices query
* 索引查询
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected static IndicesQueryBuilder indicesQuery() {
// Using another query when no match for the main one
QueryBuilders.indicesQuery(
QueryBuilders.termQuery("name", "葫芦3812娃"),
Es_Utils.INDEX_DEMO_01, "index2"
) //设置查询索引上执行时使用不匹配指数
.noMatchQuery(QueryBuilders.termQuery("age", "葫芦3812娃"));
// Using all (match all) or none (match no documents)
return QueryBuilders.indicesQuery(
QueryBuilders.termQuery("name", "葫芦3812娃"),
Es_Utils.INDEX_DEMO_01, "index2"
) // 设置不匹配查询,可以是 all 或者 none
.noMatchQuery("none");
}
public static void main(String[] args) {
Es_Utils.startupClient();
try {
searchTest(indicesQuery());
} catch (Exception e) {
e.printStackTrace();
} finally {
Es_Utils.shutDownClient();
}
}
private static void searchTest(QueryBuilder queryBuilder) {
//预准备执行搜索
Es_Utils.client.prepareSearch(Es_Utils.INDEX_DEMO_01)
.setTypes(Es_Utils.INDEX_DEMO_01_MAPPING)
.setQuery(queryBuilder)
.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) {
}
});
}
}
发表评论
-
是什么java多态
2018-11-14 13:43 723Java多态 例1.1+1=2 例2."1&quo ... -
经纬度转换
2018-09-25 13:57 0package com.teamdev.jxbrowser.c ... -
java连接池技术
2018-09-04 14:26 402转:https://www.cnblogs.com/xdp-g ... -
java 提取url字符串中的域名
2018-08-30 15:24 7410package com.teamdev.jxbrowser.c ... -
java多线程中锁(lock)、wait、notify()的使用
2018-07-13 11:31 5770package com.kai.多线程间的通信; impor ... -
java多线程间的通信实例
2018-07-13 11:00 1445----线程间的通信 public class ListAd ... -
volatile关键字
2018-07-12 11:20 664volatile:使变量在多个线程间可见 public c ... -
java根据百度url获取真正的网页地址
2018-07-09 11:26 1115/** * 根据百度url,获取原本url * @th ... -
java中的base64字符流与图片的相互转换
2018-06-29 13:27 1289//base64字符串转化成图片 public sta ... -
java去除图片水印的解决办法
2018-06-28 10:54 3382原文地址:http://www.oicqzone.com/pc ... -
java中的中文字符转URLEncode
2018-06-26 11:26 816String urlTypeName =java.net.UR ... -
java下载图片到本地实例
2018-06-25 15:34 1374public static void main(String[ ... -
Myeclipse中如何更改jsp默认的打开方式
2018-05-04 17:44 6751.打开myeclipse 2.点击上方的 window-&g ... -
java三大范式
2018-05-02 09:50 779参考地址:https://www.cnblogs.com/ja ... -
请求中设置代理IP
2018-04-18 16:24 992(不能直接JVM设置代理 System.getProperti ... -
java获取当前时间的时间戳
2018-04-08 09:25 1521package com.teamdev.jxbrowser.c ... -
java四种线程池实例 (kaigege)
2018-03-28 11:03 735Java通过Executors提供四种线程池: 1.newCa ... -
java获取IP地址(windos,linux)皆可
2018-03-28 09:44 832package com.teamdev.jxbrowser.c ... -
post 请求
2018-02-05 16:32 547public static void trustEveryo ... -
java robot控制鼠标和键盘(笔记本中打印 i love you)(滑动)
2018-01-25 16:57 2124package robot_java; import jav ...
相关推荐
标签《ES Java API 中文文档》强调了文档的内容属性,它属于ElasticSearch的一个重要组成部分,即用Java语言进行数据交互和操作的应用程序接口部分。 从部分内容中可以提取出以下知识点: 1. **Transport Client**...
在Java环境中操作Elasticsearch,我们通常会利用官方提供的Java API,这是一个非常全面且强大的工具集,让我们能够方便地进行索引管理、查询、过滤、分组和映射设置等操作。 首先,让我们详细了解如何使用Java API...
在Java中操作Elasticsearch,首先需要添加对应的依赖库。Elasticsearch提供了官方的Java客户端,通过Maven或Gradle可以轻松引入。例如,在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml <groupId>org....
7. **Java REST客户端的使用**:`RestHighLevelClient`是Elasticsearch提供的高级Java客户端,提供了丰富的API用于操作索引、文档、搜索等。创建客户端实例需要提供ES服务器的URL,可以设置连接参数如超时、重试策略...
以上代码片段展示了如何在Java中封装Elasticsearch的基本操作。为了确保线程安全,你可能需要将这些方法放在一个静态工具类中,并使用`try-with-resources`处理`RestHighLevelClient`的关闭。同时,根据实际需求,你...
Java API是Elasticsearch官方提供的与Elasticsearch服务器进行交互的主要工具,它使得开发者能够用Java语言便捷地进行索引、搜索、聚合等多种操作。 ### 一、Elasticsearch核心概念 1. **节点(Node)**: Elastic...
最后,`es`包可能是Elasticsearch相关的操作接口或抽象类,它们定义了与Elasticsearch交互的方法,如添加、更新、删除文档,以及查询等。例如: ```java public interface ElasticsearchRepository { void save...
(狂神)ElasticSearch快速入门笔记,ElasticSearch基本操作以及爬虫(Java-ES仿京东实战),包含了小狂神讲的东西,特别适合新手学习,笔记保存下来可以多看看。好记性不如烂笔头哦~,ElasticSearch,简称es,es是一个...
本主题聚焦于“Elasticsearch Java代码实现”,将深入探讨如何使用Java API来执行基本的操作,如创建索引、删除索引、更新索引、模糊搜索以及模糊全文搜索和精确查找。 首先,让我们从`ESManager.java`开始,这个类...
Elasticsearch提供了Java REST客户端,允许开发者直接通过Java代码与ES集群进行交互。这个工具类可能包含了如索引文档、查询、更新和删除文档等基本操作的封装,以及聚合、脚本等高级功能。例如,`IndexRequest`用于...
在这个基于Elasticsearch 2.1.1的Java API基本操作代码示例中,我们将探讨如何利用Java API进行常见的数据操作,如索引创建、文档插入、查询以及更新。 首先,为了使用Elasticsearch的Java API,我们需要在项目中...
**Elasticsearch Java API**是Elasticsearch官方提供的用于与Elasticsearch服务器进行交互的Java客户端库。在Java应用程序中,我们通常会使用这个API来创建、查询、更新和删除索引中的数据。Elasticsearch 2.3版本的...
java操作ElasticSearch的工具类。需要添加依赖: <!-- ElasticSearch依赖 --> <groupId>io.searchbox <artifactId>jest <version>6.3.1 <!-- ...
在 Java 应用程序中使用 Elasticsearch,开发者可以利用 Java API 来执行索引、搜索、聚合、更新和删除等操作。这些操作使得 Elasticsearch 成为大数据分析、日志处理和实时搜索等场景的理想选择。 **1. 索引操作**...
Java做客户端对Elasticsearch服务的增删改查及批量修改操作,代码简洁易懂,思路清晰有注释.详情参考https://blog.csdn.net/linhaiyun_ytdx/article/category/7042758
Elasticsearch Java Client是Elasticsearch官方提供的Java语言客户端,它提供了丰富的API供开发者使用,涵盖了索引管理、搜索、聚合、更新、删除等操作。这个客户端不仅包含了低级别的RESTful通信,还提供了一种更...
在Java环境中使用Elasticsearch,通常会涉及到Elasticsearch官方提供的Java客户端库,它允许开发者通过Java代码操作Elasticsearch集群,实现数据的索引、查询、聚合、更新以及删除等操作。Java客户端通过REST API与...
elasticsearch java工具包 提供各种elasticsearch 操作,可直接应用于项目
在Java环境中,我们可以使用官方提供的Java API来与Elasticsearch进行交互,实现数据的索引、查询、更新和删除等操作。 **一、Elasticsearch Java API** 1. **安装与配置**: 首先,你需要在项目中引入Elastic...