ElasticSearch6.x 基于SpringBoot 实现ElasticSearch全文查询

Myth丶恋晨 2023-10-18 22:24 214阅读 0赞

elasticsearch6.x 官网文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-full-text-queries.html

SpringBoot 功能封装涉及ElasticSearch的全文检索方法约定如下:

public SearchResponse searchMatchQuery(String[] indexs, String[] types,String name, Object text)

public SearchResponse searchMultiMatchQuery(String[] indexs, String[] types,String[] fieldNames, Object text)

public SearchResponse searchCommonTermsQuery(String[] indexs, String[] types,String fieldName, Object text)

public SearchResponse searchQueryStringQuery(String[] indexs, String[] types,String queryString)

在上一篇文中说到:ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装,将约定的方法填充到ElasticSearchIndexUtil.java 工具类中。

  1. @Component
  2. public class ElasticSearchIndexUtil {
  3. // 引入 Ela 连接实列化对象
  4. @Autowired
  5. private TransportClient client;
  6. /**
  7. * 功能描述:全文检索之match query
  8. * @param indexs 索引数组
  9. * @param types 类型数组
  10. * @param name 文档属性名称
  11. * @param text 文档属性值
  12. * @return
  13. * @throws InterruptedException
  14. * @throws ExecutionException
  15. */
  16. public SearchResponse searchMatchQuery(String[] indexs, String[] types,String name, Object text) throws InterruptedException, ExecutionException{
  17. MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(name, text);
  18. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(matchQuery).execute().get();
  19. return response;
  20. }
  21. /**
  22. * 功能描述:全文检索之multi_match query
  23. * @param indexs 索引数组
  24. * @param types 类型数组
  25. * @param fieldNames 文档属性数组
  26. * @param text 文档属性值
  27. * @return
  28. * @throws InterruptedException
  29. * @throws ExecutionException
  30. */
  31. public SearchResponse searchMultiMatchQuery(String[] indexs, String[] types,String[] fieldNames, Object text) throws InterruptedException, ExecutionException{
  32. MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(text, fieldNames);
  33. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(multiMatchQuery).execute().get();
  34. return response;
  35. }
  36. /**
  37. * 功能描述:全文检索之common_terms query
  38. * @param indexs 索引数组
  39. * @param types 类型数组
  40. * @param fieldNames 文档属性数组
  41. * @param text 文档属性值
  42. * @return
  43. * @throws InterruptedException
  44. * @throws ExecutionException
  45. */
  46. public SearchResponse searchCommonTermsQuery(String[] indexs, String[] types,String fieldName, Object text) throws InterruptedException, ExecutionException{
  47. CommonTermsQueryBuilder commonTermsQuery = QueryBuilders.commonTermsQuery(fieldName, text);
  48. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(commonTermsQuery).execute().get();
  49. return response;
  50. }
  51. /**
  52. * 功能描述:全文检索之query_string query
  53. * @param indexs 索引数组
  54. * @param types 类型数组
  55. * @param fieldNames 文档属性数组
  56. * @param text 文档属性值
  57. * @return
  58. * @throws InterruptedException
  59. * @throws ExecutionException
  60. */
  61. public SearchResponse searchQueryStringQuery(String[] indexs, String[] types,String queryString) throws InterruptedException, ExecutionException{
  62. QueryStringQueryBuilder queryStringQuery = QueryBuilders.queryStringQuery(queryString);
  63. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(queryStringQuery).execute().get();
  64. return response;
  65. }
  66. /**
  67. * 功能描述:全文检索之simple_query_string
  68. * @param indexs 索引数组
  69. * @param types 类型数组
  70. * @param fieldNames 文档属性数组
  71. * @param text 文档属性值
  72. * @return
  73. * @throws InterruptedException
  74. * @throws ExecutionException
  75. */
  76. public SearchResponse searchSimpleQueryStringQuery(String[] indexs, String[] types,String queryString) throws InterruptedException, ExecutionException{
  77. SimpleQueryStringBuilder simpleQueryString = QueryBuilders.simpleQueryStringQuery(queryString);
  78. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(simpleQueryString).execute().get();
  79. return response;
  80. }
  81. }

功能的测试方法就不进行补充了

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读