普通查询和正则表达式的弹性搜索查询生成

Elastic Search query Generation for normal query and regular expression

我将学生信息放在 Elastic 搜索数据库中。我正在尝试执行搜索

  1. 获取学生成绩为“A”的所有记录

     {
        "query":{
           "bool":{
              "must":[
                 {
                    "match":{
                       "grade":{
                          "query":"A",
                          "operator":"OR",
                          "prefix_length":0,
                          "max_expansions":50,
                          "fuzzy_transpositions":true,
                          "lenient":false,
                          "zero_terms_query":"NONE",
                          "auto_generate_synonyms_phrase_query":true,
                          "boost":1.0
                       }
                    }
                 }
              ],
              "adjust_pure_negative":true,
              "boost":1.0
           }
        }
     }
    
  2. 获取城市值以B*开头的所有记录

     {
        "query":{
           "bool":{
              "must":[
                 {
                    "regexp":{
                       "city":{
                          "value":"B*",
                          "flags_value":65535,
                          "max_determinized_states":10000,
                          "boost":1.0
                       }
                    }
                 }
              ],
              "adjust_pure_negative":true,
              "boost":1.0
           }
        }
     }
    
  3. 用于生成搜索查询

     SearchSourceBuilder elasticSearchSourceBuilder = new SearchSourceBuilder();
     BoolQueryBuilder esQueryBuilder = QueryBuilders.boolQuery();
     Map<String,List<String>> searchCriteriaMap = new HashMap();
     searchCriteriaMap.put("grade", Arrays.asList(new String[] {"A"}));
     for(Map.Entry<String, List<String>> entry :searchCriteriaMap.entrySet()) {
         List<String> parameterValueList = searchCriteriaMap.get(entry.getKey());
         String parameterValue = String.join(",", parameterValueList);
         MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery(entry.getKey(), parameterValue);
    
         esQueryBuilder.must(matchQueryBuilder);
    
     }
    
  4. 用于生成正则表达式查询:

     SearchSourceBuilder elasticSearchSourceBuilder = new SearchSourceBuilder();
     BoolQueryBuilder esQueryBuilder = QueryBuilders.boolQuery();
     Map<String,List<String>> searchCriteriaMap = new HashMap();
     searchCriteriaMap.put("city", Arrays.asList(new String[] {"Bangalore*"}));
     //queryParameterMap1.put("school.name", Arrays.asList(new String[] {"Joseph"}));
     for(Map.Entry<String, List<String>> entry :searchCriteriaMap.entrySet()) {  
         RegexpQueryBuilder matchQueryBuilder= QueryBuilders.regexpQuery("city", "Bang*");
         esQueryBuilder.must(matchQueryBuilder);
    
     }
    

如何在单个查询中处理这两种情况,即成绩为 A 的学生和城市值应以 B* 开头。为了生成查询,我使用了 Java Elastic search query Builder。

学生记录为:

{
   "name":"Anthon",
   "id":"JO-01",
   "courses":[
      "English",
      "Science",
      "Mathematics",
      "Physics",
      "Biology"
   ],
   "grade":"A",
   "cgpa":8.3,
   "schoolName":"Joseph Higher Seconday School",
   "city":"Bangalore",
   "dateOfJoining":"Jul 24, 3914 12:00:00 AM"
}

您可以将两个查询的 must 子句组合起来

查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "grade": {
              "query": "A"
            }
          }
        },
        {
          "regexp": {
            "city.keyword": {
              "value": "B.*"
            }
          }
        }
      ]
    }
  }
}

Java代码:

SearchSourceBuilder elasticSearchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder esQueryBuilder = QueryBuilders.boolQuery();
MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery("grade", "A");
RegexpQueryBuilder regxQueryBuilder= QueryBuilders.regexpQuery("city", "B*");
esQueryBuilder.must(matchQueryBuilder);
esQueryBuilder.must(regxQueryBuilder);
System.out.println(esQueryBuilder.toString());

在这里,如果你使用 multi-field 那么你需要给出 city.keyword 并且你需要传递与字段值相同的查询 B.* 并且如果你将城市字段定义为text 然后你需要使用 city 作为字段名称和值作为 b.*