当前位置:   article > 正文

elasticsearch -- 使用QueryBuilders进行查询_multimatchquerybuilder

multimatchquerybuilder

/**

 * 系统环境: vm12 下的centos 7.2

 * 当前安装版本: elasticsearch-2.4.0.tar.gz

 */

QueryBuilder 是es中提供的一个查询接口, 可以对其进行参数设置来进行查用擦还训

  1. package com.wenbronk.javaes;
  2. import java.net.InetSocketAddress;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Map.Entry;
  6. import org.elasticsearch.action.ListenableActionFuture;
  7. import org.elasticsearch.action.get.GetRequestBuilder;
  8. import org.elasticsearch.action.get.GetResponse;
  9. import org.elasticsearch.action.search.SearchResponse;
  10. import org.elasticsearch.action.search.SearchType;
  11. import org.elasticsearch.client.transport.TransportClient;
  12. import org.elasticsearch.common.settings.Settings;
  13. import org.elasticsearch.common.text.Text;
  14. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  15. import org.elasticsearch.common.unit.TimeValue;
  16. import org.elasticsearch.index.query.IndicesQueryBuilder;
  17. import org.elasticsearch.index.query.NestedQueryBuilder;
  18. import org.elasticsearch.index.query.QueryBuilder;
  19. import org.elasticsearch.index.query.QueryBuilders;
  20. import org.elasticsearch.index.query.QueryStringQueryBuilder;
  21. import org.elasticsearch.index.query.RangeQueryBuilder;
  22. import org.elasticsearch.index.query.SpanFirstQueryBuilder;
  23. import org.elasticsearch.index.query.WildcardQueryBuilder;
  24. import org.elasticsearch.search.SearchHit;
  25. import org.elasticsearch.search.SearchHits;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. /**
  29. * java操作查询api
  30. * @author 231
  31. *
  32. */
  33. public class JavaESQuery {
  34. private TransportClient client;
  35. @Before
  36. public void testBefore() {
  37. Settings settings = Settings.settingsBuilder().put("cluster.name", "wenbronk_escluster").build();
  38. client = TransportClient.builder().settings(settings).build()
  39. .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.50.37", 9300)));
  40. System.out.println("success to connect escluster");
  41. }
  42. /**
  43. * 使用get查询
  44. */
  45. @Test
  46. public void testGet() {
  47. GetRequestBuilder requestBuilder = client.prepareGet("twitter", "tweet", "1");
  48. GetResponse response = requestBuilder.execute().actionGet();
  49. GetResponse getResponse = requestBuilder.get();
  50. ListenableActionFuture<GetResponse> execute = requestBuilder.execute();
  51. System.out.println(response.getSourceAsString());
  52. }
  53. /**
  54. * 使用QueryBuilder
  55. * termQuery("key", obj) 完全匹配
  56. * termsQuery("key", obj1, obj2..) 一次匹配多个值
  57. * matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
  58. * multiMatchQuery("text", "field1", "field2"..); 匹配多个字段, field有通配符忒行
  59. * matchAllQuery(); 匹配所有文件
  60. */
  61. @Test
  62. public void testQueryBuilder() {
  63. // QueryBuilder queryBuilder = QueryBuilders.termQuery("user", "kimchy");
  64.       QueryBUilder queryBuilder = QueryBuilders.termQuery("user", "kimchy", "wenbronk", "vini");
  65. QueryBuilders.termsQuery("user", new ArrayList<String>().add("kimchy"));
  66. // QueryBuilder queryBuilder = QueryBuilders.matchQuery("user", "kimchy");
  67. // QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("kimchy", "user", "message", "gender");
  68. QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
  69. searchFunction(queryBuilder);
  70. }
  71. /**
  72. * 组合查询
  73. * must(QueryBuilders) : AND
  74. * mustNot(QueryBuilders): NOT
  75. * should: : OR
  76. */
  77. @Test
  78. public void testQueryBuilder2() {
  79. QueryBuilder queryBuilder = QueryBuilders.boolQuery()
  80. .must(QueryBuilders.termQuery("user", "kimchy"))
  81. .mustNot(QueryBuilders.termQuery("message", "nihao"))
  82. .should(QueryBuilders.termQuery("gender", "male"));
  83. searchFunction(queryBuilder);
  84. }
  85. /**
  86. * 只查询一个id的
  87. * QueryBuilders.idsQuery(String...type).ids(Collection<String> ids)
  88. */
  89. @Test
  90. public void testIdsQuery() {
  91. QueryBuilder queryBuilder = QueryBuilders.idsQuery().ids("1");
  92. searchFunction(queryBuilder);
  93. }
  94. /**
  95. * 包裹查询, 高于设定分数, 不计算相关性
  96. */
  97. @Test
  98. public void testConstantScoreQuery() {
  99. QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "kimchy")).boost(2.0f);
  100. searchFunction(queryBuilder);
  101. // 过滤查询
  102. // QueryBuilders.constantScoreQuery(FilterBuilders.termQuery("name", "kimchy")).boost(2.0f);
  103. }
  104. /**
  105. * disMax查询
  106. * 对子查询的结果做union, score沿用子查询score的最大值,
  107. * 广泛用于muti-field查询
  108. */
  109. @Test
  110. public void testDisMaxQuery() {
  111. QueryBuilder queryBuilder = QueryBuilders.disMaxQuery()
  112. .add(QueryBuilders.termQuery("user", "kimch")) // 查询条件
  113. .add(QueryBuilders.termQuery("message", "hello"))
  114. .boost(1.3f)
  115. .tieBreaker(0.7f);
  116. searchFunction(queryBuilder);
  117. }
  118. /**
  119. * 模糊查询
  120. * 不能用通配符, 不知道干啥用
  121. */
  122. @Test
  123. public void testFuzzyQuery() {
  124. QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("user", "kimch");
  125. searchFunction(queryBuilder);
  126. }
  127. /**
  128. * 父或子的文档查询
  129. */
  130. @Test
  131. public void testChildQuery() {
  132. QueryBuilder queryBuilder = QueryBuilders.hasChildQuery("sonDoc", QueryBuilders.termQuery("name", "vini"));
  133. searchFunction(queryBuilder);
  134. }
  135. /**
  136. * moreLikeThisQuery: 实现基于内容推荐, 支持实现一句话相似文章查询
  137. * {
  138. "more_like_this" : {
  139. "fields" : ["title", "content"], // 要匹配的字段, 不填默认_all
  140. "like_text" : "text like this one", // 匹配的文本
  141. }
  142. }
  143. percent_terms_to_match:匹配项(term)的百分比,默认是0.3
  144. min_term_freq:一篇文档中一个词语至少出现次数,小于这个值的词将被忽略,默认是2
  145. max_query_terms:一条查询语句中允许最多查询词语的个数,默认是25
  146. stop_words:设置停止词,匹配时会忽略停止词
  147. min_doc_freq:一个词语最少在多少篇文档中出现,小于这个值的词会将被忽略,默认是无限制
  148. max_doc_freq:一个词语最多在多少篇文档中出现,大于这个值的词会将被忽略,默认是无限制
  149. min_word_len:最小的词语长度,默认是0
  150. max_word_len:最多的词语长度,默认无限制
  151. boost_terms:设置词语权重,默认是1
  152. boost:设置查询权重,默认是1
  153. analyzer:设置使用的分词器,默认是使用该字段指定的分词器
  154. */
  155. @Test
  156. public void testMoreLikeThisQuery() {
  157. QueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("user")
  158. .like("kimchy");
  159. // .minTermFreq(1) //最少出现的次数
  160. // .maxQueryTerms(12); // 最多允许查询的词语
  161. searchFunction(queryBuilder);
  162. }
  163. /**
  164. * 前缀查询
  165. */
  166. @Test
  167. public void testPrefixQuery() {
  168. QueryBuilder queryBuilder = QueryBuilders.matchQuery("user", "kimchy");
  169. searchFunction(queryBuilder);
  170. }
  171. /**
  172. * 查询解析查询字符串
  173. */
  174. @Test
  175. public void testQueryString() {
  176. QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("+kimchy");
  177. searchFunction(queryBuilder);
  178. }
  179. /**
  180. * 范围内查询
  181. */
  182. public void testRangeQuery() {
  183. QueryBuilder queryBuilder = QueryBuilders.rangeQuery("user")
  184. .from("kimchy")
  185. .to("wenbronk")
  186. .includeLower(true) // 包含上界
  187. .includeUpper(true); // 包含下届
  188. searchFunction(queryBuilder);
  189. }
  190. /**
  191. * 跨度查询
  192. */
  193. @Test
  194. public void testSpanQueries() {
  195. QueryBuilder queryBuilder1 = QueryBuilders.spanFirstQuery(QueryBuilders.spanTermQuery("name", "葫芦580娃"), 30000); // Max查询范围的结束位置
  196. QueryBuilder queryBuilder2 = QueryBuilders.spanNearQuery()
  197. .clause(QueryBuilders.spanTermQuery("name", "葫芦580娃")) // Span Term Queries
  198. .clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
  199. .clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"))
  200. .slop(30000) // Slop factor
  201. .inOrder(false)
  202. .collectPayloads(false);
  203. // Span Not
  204. QueryBuilder queryBuilder3 = QueryBuilders.spanNotQuery()
  205. .include(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
  206. .exclude(QueryBuilders.spanTermQuery("home", "山西省太原市2552街道"));
  207. // Span Or
  208. QueryBuilder queryBuilder4 = QueryBuilders.spanOrQuery()
  209. .clause(QueryBuilders.spanTermQuery("name", "葫芦580娃"))
  210. .clause(QueryBuilders.spanTermQuery("name", "葫芦3812娃"))
  211. .clause(QueryBuilders.spanTermQuery("name", "葫芦7139娃"));
  212. // Span Term
  213. QueryBuilder queryBuilder5 = QueryBuilders.spanTermQuery("name", "葫芦580娃");
  214. }
  215. /**
  216. * 测试子查询
  217. */
  218. @Test
  219. public void testTopChildrenQuery() {
  220. QueryBuilders.hasChildQuery("tweet",
  221. QueryBuilders.termQuery("user", "kimchy"))
  222. .scoreMode("max");
  223. }
  224. /**
  225. * 通配符查询, 支持 *
  226. * 匹配任何字符序列, 包括空
  227. * 避免* 开始, 会检索大量内容造成效率缓慢
  228. */
  229. @Test
  230. public void testWildCardQuery() {
  231. QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("user", "ki*hy");
  232. searchFunction(queryBuilder);
  233. }
  234. /**
  235. * 嵌套查询, 内嵌文档查询
  236. */
  237. @Test
  238. public void testNestedQuery() {
  239. QueryBuilder queryBuilder = QueryBuilders.nestedQuery("location",
  240. QueryBuilders.boolQuery()
  241. .must(QueryBuilders.matchQuery("location.lat", 0.962590433140581))
  242. .must(QueryBuilders.rangeQuery("location.lon").lt(36.0000).gt(0.000)))
  243. .scoreMode("total");
  244. }
  245. /**
  246. * 测试索引查询
  247. */
  248. @Test
  249. public void testIndicesQueryBuilder () {
  250. QueryBuilder queryBuilder = QueryBuilders.indicesQuery(
  251. QueryBuilders.termQuery("user", "kimchy"), "index1", "index2")
  252. .noMatchQuery(QueryBuilders.termQuery("user", "kimchy"));
  253. }
  254. /**
  255. * 查询遍历抽取
  256. * @param queryBuilder
  257. */
  258. private void searchFunction(QueryBuilder queryBuilder) {
  259. SearchResponse response = client.prepareSearch("twitter")
  260. .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  261. .setScroll(new TimeValue(60000))
  262. .setQuery(queryBuilder)
  263. .setSize(100).execute().actionGet();
  264. while(true) {
  265. response = client.prepareSearchScroll(response.getScrollId())
  266. .setScroll(new TimeValue(60000)).execute().actionGet();
  267. for (SearchHit hit : response.getHits()) {
  268. Iterator<Entry<String, Object>> iterator = hit.getSource().entrySet().iterator();
  269. while(iterator.hasNext()) {
  270. Entry<String, Object> next = iterator.next();
  271. System.out.println(next.getKey() + ": " + next.getValue());
  272. if(response.getHits().hits().length == 0) {
  273. break;
  274. }
  275. }
  276. }
  277. break;
  278. }
  279. // testResponse(response);
  280. }
  281. /**
  282. * 对response结果的分析
  283. * @param response
  284. */
  285. public void testResponse(SearchResponse response) {
  286. // 命中的记录数
  287. long totalHits = response.getHits().totalHits();
  288. for (SearchHit searchHit : response.getHits()) {
  289. // 打分
  290. float score = searchHit.getScore();
  291. // 文章id
  292. int id = Integer.parseInt(searchHit.getSource().get("id").toString());
  293. // title
  294. String title = searchHit.getSource().get("title").toString();
  295. // 内容
  296. String content = searchHit.getSource().get("content").toString();
  297. // 文章更新时间
  298. long updatetime = Long.parseLong(searchHit.getSource().get("updatetime").toString());
  299. }
  300. }
  301. /**
  302. * 对结果设置高亮显示
  303. */
  304. public void testHighLighted() {
  305. /* 5.0 版本后的高亮设置
  306. * client.#().#().highlighter(hBuilder).execute().actionGet();
  307. HighlightBuilder hBuilder = new HighlightBuilder();
  308. hBuilder.preTags("<h2>");
  309. hBuilder.postTags("</h2>");
  310. hBuilder.field("user"); // 设置高亮显示的字段
  311. */
  312. // 加入查询中
  313. SearchResponse response = client.prepareSearch("blog")
  314. .setQuery(QueryBuilders.matchAllQuery())
  315. .addHighlightedField("user") // 添加高亮的字段
  316. .setHighlighterPreTags("<h1>")
  317. .setHighlighterPostTags("</h1>")
  318. .execute().actionGet();
  319. // 遍历结果, 获取高亮片段
  320. SearchHits searchHits = response.getHits();
  321. for(SearchHit hit:searchHits){
  322. System.out.println("String方式打印文档搜索内容:");
  323. System.out.println(hit.getSourceAsString());
  324. System.out.println("Map方式打印高亮内容");
  325. System.out.println(hit.getHighlightFields());
  326. System.out.println("遍历高亮集合,打印高亮片段:");
  327. Text[] text = hit.getHighlightFields().get("title").getFragments();
  328. for (Text str : text) {
  329. System.out.println(str.string());
  330. }
  331. }
  332. }
  333. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/68444
推荐阅读
相关标签
  

闽ICP备14008679号