/src/main/java/org/springframework/boot/elasticsearch/ais/staticString/Find.java

https://github.com/lihang212010/Elasticsearch-ais · Java · 991 lines · 537 code · 50 blank · 404 comment · 77 complexity · 5f0ba63cc23c444c5147a2f9a7e7f8af MD5 · raw file

  1. package org.springframework.boot.elasticsearch.ais.staticString;
  2. import com.alibaba.fastjson.JSON;
  3. import org.springframework.boot.elasticsearch.ais.utils.CollectUtil;
  4. import org.springframework.boot.elasticsearch.ais.utils.StringUtils;
  5. /**
  6. * . find operation
  7. *
  8. * @author lihang
  9. * @email 631533483@qq.com
  10. * @return String script
  11. */
  12. public class Find {
  13. /**
  14. * 查询所有 find all.
  15. *
  16. * @return String script
  17. */
  18. public static String findAll() {
  19. String query = "{\n"
  20. + " \"match_all\": {}\n"
  21. + " }";
  22. return query;
  23. }
  24. /**
  25. * 精确查询 find term.
  26. *
  27. * @param key field name
  28. * @param value field value
  29. * @return String script
  30. */
  31. public static String term(String key, Object value) {
  32. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  33. String query = "{\n"
  34. + " \"term\": {\n"
  35. + " \"" + key + "\": {\n"
  36. + " \"value\": " + JSON.toJSONString(value) + "\n"
  37. + " }\n"
  38. + " }\n"
  39. + " }";
  40. return query;
  41. }
  42. return null;
  43. }
  44. /**
  45. * 精确查询 find term.
  46. *
  47. * @param key field name
  48. * @param value field value
  49. * @param boost Query ranking score
  50. * @return String script
  51. */
  52. public static String term(String key, Object value, double boost) {
  53. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  54. String query = "{\n"
  55. + " \"term\": {\n"
  56. + " \"" + key + "\": {\n"
  57. + " \"value\": " + JSON.toJSONString(value) + ", \n"
  58. + " \"boost\": " + boost + "\n"
  59. + " }\n"
  60. + " }\n"
  61. + " }";
  62. return query;
  63. }
  64. return null;
  65. }
  66. /**
  67. * 分词查询 find match.
  68. *
  69. * @param key field name
  70. * @param value field value
  71. * @return String script
  72. */
  73. public static String match(String key, Object value) {
  74. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  75. String query = "{\n"
  76. + " \"match\": {\n"
  77. + " \"" + key + "\": \"" + value.toString().trim() + "\"\n"
  78. + " }\n"
  79. + " }";
  80. return query;
  81. }
  82. return null;
  83. }
  84. /**
  85. * 分词查询 find match.
  86. *
  87. * @param key field name
  88. * @param value field value
  89. * @param boost Query ranking score
  90. * @return String script
  91. */
  92. public static String match(String key, Object value, double boost) {
  93. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  94. String query = " {\n"
  95. + " \"match\": {\n"
  96. + " \"" + key + "\": {\n"
  97. + " \"boost\": " + boost + ",\n"
  98. + " \"query\": \"" + value.toString().trim() + "\"\n"
  99. + " }\n"
  100. + " }\n"
  101. + " } ";
  102. return query;
  103. }
  104. return null;
  105. }
  106. /**
  107. * 模糊查询 find wildcard,Left and right have been added* .
  108. *
  109. * @param key field name
  110. * @param value field value
  111. * @return String script
  112. */
  113. public static String wildcard(String key, Object value) {
  114. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  115. String query = "{\n"
  116. + " \"wildcard\": {\n"
  117. + " \"" + key + "\": {\n"
  118. + " \"value\": \"*" + value.toString().trim() + "*\"\n"
  119. + " }\n"
  120. + " }\n"
  121. + " }";
  122. return query;
  123. }
  124. return null;
  125. }
  126. /**
  127. * 模糊查询 find wildcard,Left and right have been added * .
  128. *
  129. * @param key field name
  130. * @param value field value
  131. * @param boost boost Query ranking score
  132. * @return String script
  133. */
  134. public static String wildcard(String key, Object value, double boost) {
  135. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  136. String query = "{\n"
  137. + " \"wildcard\": {\n"
  138. + " \"" + key + "\": {\n"
  139. + " \"value\": \"*" + value.toString().trim() + "*\",\n"
  140. + " \"boost\": " + boost + "\n"
  141. + " }\n"
  142. + " }\n"
  143. + " } ";
  144. return query;
  145. }
  146. return null;
  147. }
  148. /**
  149. * 模糊查询 find wildcard,Left have been added * .
  150. *
  151. * @param key field name
  152. * @param value field value
  153. * @return String script
  154. */
  155. public static String wildcardLeft(String key, Object value) {
  156. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  157. String query = "{\n"
  158. + " \"wildcard\": {\n"
  159. + " \"" + key + "\": {\n"
  160. + " \"value\": \"*" + value.toString().trim() + "\"\n"
  161. + " }\n"
  162. + " }\n"
  163. + " }";
  164. return query;
  165. }
  166. return null;
  167. }
  168. /**
  169. * 模糊查询 find wildcard,Left have been added * .
  170. *
  171. * @param key field name
  172. * @param value field value
  173. * @param boost boost Query ranking score
  174. * @return String script
  175. */
  176. public static String wildcardLeft(String key, Object value, double boost) {
  177. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  178. String query = "{\n"
  179. + " \"wildcard\": {\n"
  180. + " \"" + key + "\": {\n"
  181. + " \"value\": \"*" + value.toString().trim() + "\",\n"
  182. + " \"boost\": " + boost + "\n"
  183. + " }\n"
  184. + " }\n"
  185. + " } ";
  186. return query;
  187. }
  188. return null;
  189. }
  190. /**
  191. * 模糊查询 find wildcard,Right have been added * .
  192. *
  193. * @param key field name
  194. * @param value field value
  195. * @return String script
  196. */
  197. public static String wildcardRight(String key, Object value) {
  198. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  199. String query = "{\n"
  200. + " \"wildcard\": {\n"
  201. + " \"" + key + "\": {\n"
  202. + " \"value\": \"" + value.toString().trim() + "*\"\n"
  203. + " }\n"
  204. + " }\n"
  205. + " }";
  206. return query;
  207. }
  208. return null;
  209. }
  210. /**
  211. * 模糊查询 find wildcard,Right have been added * .
  212. *
  213. * @param key field name
  214. * @param value field value
  215. * @param boost boost Query ranking score
  216. * @return String script
  217. */
  218. public static String wildcardRight(String key, Object value, double boost) {
  219. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  220. String query = "{\n"
  221. + " \"wildcard\": {\n"
  222. + " \"" + key + "\": {\n"
  223. + " \"value\": \"" + value.toString().trim() + "*\",\n"
  224. + " \"boost\": " + boost + "\n"
  225. + " }\n"
  226. + " }\n"
  227. + " } ";
  228. return query;
  229. }
  230. return null;
  231. }
  232. /**
  233. * 精确数组查询 find terms .
  234. *
  235. * @param key field name
  236. * @param value field value
  237. * @return String script
  238. */
  239. public static String trems(String key, Object... value) {
  240. if (StringUtils.isNotBlank(key) && CollectUtil.isNotEmpty(value)) {
  241. String query = "{\n"
  242. + " \"terms\": {\n"
  243. + " \"" + key + "\":[" + CollectUtil.commaSplit(value) + "]\n"
  244. + " }\n"
  245. + " }";
  246. return query;
  247. }
  248. return null;
  249. }
  250. /**
  251. * 模糊查询 Left and right not added * .
  252. *
  253. * @param key field name
  254. * @param value field value
  255. * @return String script
  256. */
  257. public static String wildcardFree(String key, Object value) {
  258. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  259. String query = "{\n"
  260. + " \"wildcard\": {\n"
  261. + " \"" + key + "\": {\n"
  262. + " \"value\": \"" + value.toString().trim() + "\"\n"
  263. + " }\n"
  264. + " }\n"
  265. + " }";
  266. return query;
  267. }
  268. return null;
  269. }
  270. /**
  271. * 模糊查询 Left and right not added * .
  272. *
  273. * @param key field name
  274. * @param value field value
  275. * @param boost boost Query ranking score
  276. * @return String script
  277. */
  278. public static String wildcardFree(String key, Object value, double boost) {
  279. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  280. String query = "{\n"
  281. + " \"wildcard\": {\n"
  282. + " \"" + key + "\": {\n"
  283. + " \"value\": \"" + value.toString().trim() + "\",\n"
  284. + " \"boost\": " + boost + "\n"
  285. + " }\n"
  286. + " }\n"
  287. + " } ";
  288. return query;
  289. }
  290. return null;
  291. }
  292. /**
  293. * 短语前缀匹配查询.
  294. *
  295. * <p>Returns documents that contain the words of a provided text,
  296. * in the same order as provided. The last term of the provided text is treated as a prefix,
  297. * matching any words that begin with that term.<p/>
  298. *
  299. * @param key field name
  300. * @param value field value
  301. * @param slop slop(Optional, integer) Maximum number of positions allowed between matching
  302. * tokens. Defaults to 0. Transposed terms have a slop of 2.
  303. * @return String script
  304. */
  305. public static String match_phrase_prefix(String key, Object value, int slop) {
  306. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  307. String query = "{\n"
  308. + " \"match_phrase_prefix\": {\n"
  309. + " \"" + key + "\": {\n"
  310. + " \"query\": \"" + value.toString().trim() + "\",\n"
  311. + " \"max_expansions\": " + slop + "\n"
  312. + " }\n"
  313. + " }\n"
  314. + " }";
  315. return query;
  316. }
  317. return null;
  318. }
  319. /**
  320. * 短语前缀匹配查询.
  321. * <p>Returns documents that contain the words of a provided text,
  322. * in the same order as provided. The last term of the provided text is treated as a prefix,
  323. * matching any words that begin with that term.</p>
  324. *
  325. * @param key field name
  326. * @param value field value
  327. * @param slop slop(Optional, integer) Maximum number of positions allowed between
  328. * matching tokens. Defaults to 0. Transposed terms have a slop of 2.
  329. * @param max_expansions Maximum number of terms to which the last provided term of the query
  330. * value will expand.
  331. * @return String script
  332. */
  333. public static String match_phrase_prefix(String key, Object value, int max_expansions, int slop) {
  334. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  335. String query = "{\n"
  336. + " \"match_phrase_prefix\": {\n"
  337. + " \"" + key + "\": {\n"
  338. + " \"query\": \"" + value.toString().trim() + "\",\n"
  339. + " \"max_expansions\": " + max_expansions + ",\n"
  340. + " \"slop\":" + slop + "\n"
  341. + " }\n"
  342. + " }\n"
  343. + " }";
  344. return query;
  345. }
  346. return null;
  347. }
  348. /**
  349. * 短语匹配查询.
  350. * <p>The match_phrase query analyzes the text and creates a phrase query out of the analyzed
  351. * text.</p>
  352. *
  353. * @param key field name
  354. * @param value field value
  355. * @param slop slop(Optional, integer) Maximum number of positions allowed between matching
  356. * tokens. Defaults to 0. Transposed terms have a slop of 2.
  357. * @return String script
  358. */
  359. public static String match_phrase(String key, Object value, int slop) {
  360. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  361. String query = "{\n"
  362. + " \"match_phrase\": {\n"
  363. + " \"" + key + "\": {\n"
  364. + " \"query\": \"" + value.toString().trim() + "\",\n"
  365. + " \"slop\": " + slop + "\n"
  366. + " }\n"
  367. + " }\n"
  368. + " }";
  369. return query;
  370. }
  371. return null;
  372. }
  373. /**
  374. * 排除高频率词查询查询.
  375. * <p>The common terms query is a modern alternative to stopwords which improves the precision
  376. * and recall of search results (by taking stopwords into account), without sacrificing
  377. * performance.</p>
  378. *
  379. * @param key field name
  380. * @param value field value
  381. * @param cutoff_frequency Terms are allocated to the high or low frequency groups based on the
  382. * cutoff_frequency, which can be specified as an absolute frequency (>=1)
  383. * or as a relative frequency (0.0 .. 1.0).
  384. * @return String script
  385. */
  386. public static String common(String key, Object value, double cutoff_frequency) {
  387. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  388. String query = " {\n"
  389. + " \"common\": {\n"
  390. + " \"" + key + "\": {\n"
  391. + " \"query\": \"" + value.toString().trim() + "\",\n"
  392. + " \"cutoff_frequency\": " + cutoff_frequency + "\n"
  393. + " }\n"
  394. + " }\n"
  395. + " }";
  396. return query;
  397. }
  398. return null;
  399. }
  400. /**
  401. * 判断字段是否存在.
  402. *
  403. * <p>exits field<p/>
  404. *
  405. * @param key field name
  406. * @return String script
  407. */
  408. public static String exits(String key) {
  409. if (StringUtils.isNotBlank(key)) {
  410. String query = " {\n"
  411. + " \"exists\": {\n"
  412. + " \"field\": \"" + key + "\"\n"
  413. + " }\n"
  414. + " }";
  415. return query;
  416. }
  417. return null;
  418. }
  419. /**
  420. * 相似查询,可以允许错误 Allow bad queries.
  421. *
  422. * @param key field name
  423. * @param value field value
  424. * @param boost boost Query ranking score
  425. * @param prefix_length Number of beginning characters left unchanged when creating expansions.
  426. * @param fuzziness Maximum edit distance allowed for matching. See Fuzziness for valid values
  427. * and more information
  428. * @return String script
  429. */
  430. public static String fuzzy(String key, Object value, int fuzziness, int prefix_length,
  431. double boost) {
  432. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  433. String query = "{\n"
  434. + " \"fuzzy\": {\n"
  435. + " \"" + key + "\": {\n"
  436. + " \"value\": \"" + value.toString().trim() + "\",\n"
  437. + " \"fuzziness\": " + fuzziness + ",\n"
  438. + " \"prefix_length\": " + prefix_length + ",\n"
  439. + " \"boost\": " + boost + "\n"
  440. + " }\n"
  441. + " }\n"
  442. + " }";
  443. return query;
  444. }
  445. return null;
  446. }
  447. /**
  448. * 相似查询,可以允许错误 Allow bad queries.
  449. *
  450. * @param key field name
  451. * @param value field value
  452. * @param prefix_length Number of beginning characters left unchanged when creating expansions.
  453. * @param fuzziness Maximum edit distance allowed for matching. See Fuzziness for valid values
  454. * and more information
  455. * @return String script
  456. */
  457. public static String fuzzy(String key, Object value, int fuzziness, int prefix_length) {
  458. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  459. String query = "{\n"
  460. + " \"fuzzy\": {\n"
  461. + " \"" + key + "\": {\n"
  462. + " \"value\": \"" + value.toString().trim() + "\",\n"
  463. + " \"fuzziness\": " + fuzziness + ",\n"
  464. + " \"prefix_length\": " + prefix_length + "\n"
  465. + " }\n"
  466. + " }\n"
  467. + " }";
  468. return query;
  469. }
  470. return null;
  471. }
  472. /**
  473. * 地理位置查询.
  474. *
  475. * <p>The geo_shape query uses the same grid square representation as the geo_shape mapping to
  476. * find documents that have a shape that intersects with the query shape. It will also use the
  477. * same Prefix Tree configuration as defined for the field mapping</p>
  478. *
  479. * @param type field name
  480. * @param coordinates coordinate
  481. * @param relation Query mode
  482. * @return String script
  483. */
  484. public static String geo_shape(String type, Object coordinates, Object relation) {
  485. if (StringUtils.isNotBlank(type) && StringUtils.isNotEmpty(coordinates)) {
  486. String query = "{\n"
  487. + " \"geo_shape\": {\n"
  488. + " \"location\": {\n"
  489. + " \"shape\": {\n"
  490. + " \"type\": \" " + type + "\",\n"
  491. + " \"coordinates\": [" + coordinates.toString().trim() + "]\n"
  492. + " }\n"
  493. + " },\n"
  494. + " \"relation\": \"" + relation + "\"\n"
  495. + " }\n"
  496. + " }";
  497. return query;
  498. }
  499. return null;
  500. }
  501. /**
  502. * id查询.
  503. *
  504. * <p>Returns documents based on their IDs. This query uses document IDs stored in the _id field.
  505. * <p/>
  506. *
  507. * @param values ids valus
  508. * @return String script
  509. */
  510. public static String ids(Object... values) {
  511. if (CollectUtil.isNotEmpty(values)) {
  512. String query = " {\n"
  513. + " \"ids\": {\n"
  514. + " \"values\": [" + CollectUtil.commaSplit(values) + "]\n"
  515. + " }\n"
  516. + " }";
  517. return query;
  518. }
  519. return null;
  520. }
  521. /**
  522. * 多字段分词查询.
  523. * <p>
  524. * The multi_match query builds on the match query to allow multi-field queries
  525. * </p>
  526. *
  527. * @param value field value
  528. * @param keys field name
  529. * @return String script
  530. */
  531. public static String multi_match(Object value, String... keys) {
  532. if (StringUtils.isNotEmpty(value) && CollectUtil.isNotEmpty(keys)) {
  533. String query = " {\n"
  534. + " \"multi_match\": {\n"
  535. + " \"query\": \"" + value + "\",\n"
  536. + " \"fields\": [" + CollectUtil.commaSplit(keys) + "]\n"
  537. + " }\n"
  538. + " }";
  539. return query;
  540. }
  541. return null;
  542. }
  543. /**
  544. * 必须包含词查询,要求字段中必须包含某个词.
  545. *
  546. * <p>The More Like This Query finds documents that are "like" a given set of documents. In order
  547. * to do so, MLT selects a set of representative terms of these input documents, forms a query
  548. * using these terms, executes the query and returns the results. The user controls the input
  549. * documents, how the terms should be selected and how the query is formed.<p/>
  550. *
  551. * @param min_term_freq The minimum term frequency below which the terms will be ignored from
  552. * the input document. Defaults to 2.
  553. * @param max_query_terms The maximum number of query terms that will be selected. Increasing this
  554. * value gives greater accuracy at the expense of query execution speed.
  555. * Defaults to 25
  556. * @param value field value
  557. * @param keys field name
  558. * @return String script
  559. */
  560. public static String more_like_this(int min_term_freq, int max_query_terms, Object value,
  561. String... keys) {
  562. if (StringUtils.isNotEmpty(value) && CollectUtil.isNotEmpty(keys)) {
  563. String query = "{\n"
  564. + " \"more_like_this\": {\n"
  565. + " \"fields\": [" + CollectUtil.commaSplit(keys) + "],\n"
  566. + " \"like\": \"" + value.toString().trim() + "\",\n"
  567. + " \"min_term_freq\": " + min_term_freq + ",\n"
  568. + " \"max_query_terms\": " + max_query_terms + "\n"
  569. + " }\n"
  570. + " }";
  571. return query;
  572. }
  573. return null;
  574. }
  575. /**
  576. * 字段属性查询.
  577. *
  578. * <p>The percolate query can be used to match queries stored in an index. The percolate query
  579. * itself contains the document that will be used as query to match with the stored queries.</p>
  580. *
  581. * @param key field name
  582. * @param value field value
  583. * @param field The field of type percolator that holds the indexed queries. This is a required
  584. * parameter.
  585. * @return String script
  586. */
  587. public static String percolate(String key, Object value, String field) {
  588. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value) && StringUtils
  589. .isNotBlank(field)) {
  590. String query = " {\n"
  591. + " \"percolate\": {\n"
  592. + " \"field\": \"" + field + "\",\n"
  593. + " \"document\": {\n"
  594. + " \"" + key + "\": \"" + value.toString().trim() + "\"\n"
  595. + " }\n"
  596. + " }\n"
  597. + " }";
  598. return query;
  599. }
  600. return null;
  601. }
  602. /**
  603. * 左模糊查询.
  604. *
  605. * <p>Returns documents that contain a specific prefix in a provided field.</p>
  606. *
  607. * @param key field name
  608. * @param value field value
  609. * @param boost boost Query ranking score
  610. * @return String script
  611. */
  612. public static String prefix(String key, Object value, double boost) {
  613. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  614. String query = " {\n"
  615. + " \"prefix\": {\n"
  616. + " \"" + key + "\": {\n"
  617. + " \"value\": \"" + value.toString().trim() + "\",\n"
  618. + " \"boost\": " + boost + "\n"
  619. + " }\n"
  620. + " }\n"
  621. + " }";
  622. return query;
  623. }
  624. return null;
  625. }
  626. /**
  627. * 左模糊查询.
  628. *
  629. * <p>Returns documents that contain a specific prefix in a provided field.</p>
  630. *
  631. * @param key field name
  632. * @param value field value
  633. * @return String script
  634. */
  635. public static String prefix(String key, Object value) {
  636. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  637. String query = " {\n"
  638. + " \"prefix\": {\n"
  639. + " \"" + key + "\": {\n"
  640. + " \"value\": \"" + value.toString().trim() + "\"\n"
  641. + " }\n"
  642. + " }\n"
  643. + " }";
  644. return query;
  645. }
  646. return null;
  647. }
  648. /**
  649. * 较为严格的字符串查询(可以进行和或非等操作,但如果查询中包含无效内容,则查询失败).
  650. *
  651. * <p>Returns documents based on a provided query string, using a parser with a strict syntax.
  652. * <p>
  653. * This query uses a syntax to parse and split the provided query string based on operators, such
  654. * as AND or NOT. The query then analyzes each split text independently before returning matching
  655. * documents.</p>
  656. *
  657. * @param default_field field name
  658. * @param query_ query script
  659. * @return String script
  660. */
  661. public static String query_string(String default_field, Object query_) {
  662. if (StringUtils.isNotBlank(default_field) && StringUtils.isNotEmpty(query_)) {
  663. String query = " {\n"
  664. + " \"query_string\": {\n"
  665. + " \"default_field\": \"" + default_field + "\",\n"
  666. + " \"query\": \"" + query_.toString().trim() + "\"\n"
  667. + " }\n"
  668. + " }";
  669. return query;
  670. }
  671. return null;
  672. }
  673. /**
  674. * 范围查询 Returns documents that contain terms within a provided range.
  675. *
  676. * @param key field name
  677. * @param gte Greater than or equal to
  678. * @param lte Less than or equal to.
  679. * @return String script
  680. */
  681. public static String range(String key, Object gte, Object lte) {
  682. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(gte) && StringUtils.isNotEmpty(lte)) {
  683. String query = "{\n"
  684. + " \"range\": {\n"
  685. + " \"" + key + "\": {\n"
  686. + " \"gte\": " + JSON.toJSONString(gte) + ",\n"
  687. + " \"lte\": " + JSON.toJSONString(lte) + "\n"
  688. + " }\n"
  689. + " }\n"
  690. + " }";
  691. return query;
  692. }
  693. return null;
  694. }
  695. /**
  696. * 范围查询 Returns documents that contain terms within a provided range.
  697. *
  698. * @param key field name
  699. * @param lte Less than or equal to.
  700. * @return String script
  701. */
  702. public static String rangelte(String key, Object lte) {
  703. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(lte)) {
  704. String query = "{\n"
  705. + " \"range\": {\n"
  706. + " \"" + key + "\": {\n"
  707. + " \"lte\": " + JSON.toJSONString(lte) + "\n"
  708. + " }\n"
  709. + " }\n"
  710. + " }";
  711. return query;
  712. }
  713. return null;
  714. }
  715. /**
  716. * 范围查询 Returns documents that contain terms within a provided range.
  717. *
  718. * @param key field name
  719. * @param gte Greater than or equal to
  720. * @return String script
  721. */
  722. public static String rangegte(String key, Object gte) {
  723. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(gte)) {
  724. String query = "{\n"
  725. + " \"range\": {\n"
  726. + " \"" + key + "\": {\n"
  727. + " \"gte\": " + JSON.toJSONString(gte) + "\n"
  728. + " }\n"
  729. + " }\n"
  730. + " }";
  731. return query;
  732. }
  733. return null;
  734. }
  735. /**
  736. * 正则查询.
  737. *
  738. * <p>Returns documents that contain terms matching a regular expression.
  739. * A regular expression is a way to match patterns in data using placeholder characters, called
  740. * operators. For a list of operators supported by the regexp query, see Regular expression
  741. * syntax.<p/>
  742. *
  743. * @param key field name
  744. * @param value field value
  745. * @param max_determinized_states Maximum number of automaton states required for the query.
  746. * Default is 10000. Elasticsearch uses Apache Lucene internally to
  747. * parse regular expressions. Lucene converts each regular
  748. * expression to a finite automaton containing a number of
  749. * determinized states. You can use this parameter to prevent that
  750. * conversion from unintentionally consuming too many resources.
  751. * You may need to increase this limit to run complex regular
  752. * expressions.
  753. * @return String script
  754. */
  755. public static String regexp(String key, Object value, int max_determinized_states) {
  756. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  757. String query = " {\n"
  758. + " \"regexp\": {\n"
  759. + " \"" + key + "\": {\n"
  760. + " \"value\": \"" + value.toString().trim() + "\",\n"
  761. + " \"max_determinized_states\": " + max_determinized_states + "\n"
  762. + " }\n"
  763. + " }\n"
  764. + " }";
  765. return query;
  766. }
  767. return null;
  768. }
  769. /**
  770. * 正则查询.
  771. *
  772. * <p>Returns documents that contain terms matching a regular expression.
  773. * A regular expression is a way to match patterns in data using placeholder characters, called
  774. * operators. For a list of operators supported by the regexp query, see Regular expression
  775. * syntax.<p/>
  776. *
  777. * @param key field name
  778. * @param value field value
  779. * @return String script
  780. */
  781. public static String regexp(String key, String value) {
  782. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value)) {
  783. String query = "{\n"
  784. + " \"regexp\": {\n"
  785. + " \"" + key + "\": {\n"
  786. + " \"value\": \"" + value.trim() + "\"\n"
  787. + " }\n"
  788. + " }\n"
  789. + " }";
  790. return query;
  791. }
  792. return null;
  793. }
  794. /**
  795. * 正则查询.
  796. *
  797. * <p>Returns documents that contain terms matching a regular expression.
  798. * A regular expression is a way to match patterns in data using placeholder characters, called
  799. * operators. For a list of operators supported by the regexp query, see Regular expression
  800. * syntax.<p/>
  801. *
  802. * @param key field name
  803. * @param value field value
  804. * @param max_determinized_states Maximum number of automaton states required for the query.
  805. * Default is 10000. Elasticsearch uses Apache Lucene internally to
  806. * parse regular expressions. Lucene converts each regular
  807. * expression to a finite automaton containing a number of
  808. * determinized states. You can use this parameter to prevent that
  809. * conversion from unintentionally consuming too many resources.
  810. * You may need to increase this limit to run complex regular
  811. * expressions.
  812. * @param flags Enables optional operators for the regular expression. For valid
  813. * values and more information, see Regular expression syntax
  814. * @return String script
  815. */
  816. public static String regexp(String key, String value, int max_determinized_states, String flags) {
  817. if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(value) && StringUtils
  818. .isNotBlank(flags)) {
  819. String query = " {\n"
  820. + " \"regexp\": {\n"
  821. + " \"" + key + "\": {\n"
  822. + " \"value\": \"" + value.trim() + "\",\n"
  823. + " \"max_determinized_states\": " + max_determinized_states + ",\n"
  824. + " \"flags\": \"" + flags + "\"\n"
  825. + " }\n"
  826. + " }\n"
  827. + " }";
  828. return query;
  829. }
  830. return null;
  831. }
  832. /**
  833. * 脚本查询 Filters documents based on a provided script. The script query is typically used in a
  834. * filter context.
  835. *
  836. * @param source Contains a script to run as a query. This script must return a boolean value,
  837. * true or false.
  838. * @return String script
  839. */
  840. public static String script(String source) {
  841. if (StringUtils.isNotBlank(source)) {
  842. String script = "{\n"
  843. + " \"script\": {\n"
  844. + " \"script\": {\n"
  845. + " \"source\": \"" + source + "\"\n"
  846. + " }\n"
  847. + " }\n"
  848. + " }";
  849. return script;
  850. }
  851. return null;
  852. }
  853. /**
  854. * 脚本查询 Filters documents based on a provided script. The script query is typically used in a
  855. * filter context.
  856. *
  857. * @param source Contains a script to run as a query. This script must return a boolean value,
  858. * true or false.
  859. * @param lang scripting language
  860. * @return String script
  861. */
  862. public static String script(String source, String lang) {
  863. if (StringUtils.isNotBlank(source) && StringUtils.isNotBlank(lang)) {
  864. String script = " {\n"
  865. + " \"script\": {\n"
  866. + " \"script\": \"" + source + "\",\n"
  867. + " \"lang\": \"" + lang + "\"\n"
  868. + " }";
  869. return script;
  870. }
  871. return null;
  872. }
  873. /**
  874. * 脚本查询 Filters documents based on a provided script. The script query is typically used in a
  875. * filter context.
  876. *
  877. * @param source Contains a script to run as a query. This script must return a boolean value,
  878. * true or false.
  879. * @param lang scripting language
  880. * @param params variable
  881. * @return String script
  882. */
  883. public static String script(String source, String lang, String params) {
  884. if (StringUtils.isNotBlank(source) && StringUtils.isNotBlank(lang)) {
  885. String script = "{\n"
  886. + " \"script\": {\n"
  887. + " \"script\": {\n"
  888. + " \"source\": \"" + source + "\",\n"
  889. + " \"lang\": \"" + lang + "\",\n"
  890. + " \"params\": {" + params + "}\n"
  891. + " }\n"
  892. + " }\n"
  893. + " }";
  894. return script;
  895. }
  896. return null;
  897. }
  898. /**
  899. * 使用较为简单的符号操作进行查询(如+-|等),查询更为严格.
  900. *
  901. * <p>Returns documents based on a provided query string, using a parser with a limited but
  902. * fault-tolerant syntax.
  903. * <p>
  904. * This query uses a simple syntax to parse and split the provided query string into terms based
  905. * on special operators. The query then analyzes each term independently before returning matching
  906. * documents.</p>
  907. *
  908. * @param query query script
  909. * @param filed field name
  910. * @return String script
  911. */
  912. public static String simple_query_string(String query, Object... filed) {
  913. if (StringUtils.isNotBlank(query) && CollectUtil.isNotEmpty(filed)) {
  914. String script = "{\n"
  915. + " \"simple_query_string\": {\n"
  916. + " \"query\": \"" + query + "\",\n"
  917. + " \"fields\": [" + CollectUtil.commaSplit(filed) + "]\n"
  918. + " }\n"
  919. + " }";
  920. return script;
  921. }
  922. return null;
  923. }
  924. /**
  925. * A query that accepts any other query as base64 encoded string.
  926. *
  927. * @param query Base64 encoded string
  928. * @return String script
  929. */
  930. public static String wripper(String query) {
  931. if (StringUtils.isNotBlank(query)) {
  932. String script = "{\n"
  933. + " \"wrapper\": {\n"
  934. + " \"query\": \"" + query + "\"\n"
  935. + " }\n"
  936. + " }";
  937. return script;
  938. }
  939. return null;
  940. }
  941. }