Elasticsearch mapping settings 'not_analyzed' and grouping by field in Java

后端 未结 1 1924
不思量自难忘°
不思量自难忘° 2021-01-27 16:47

I\'m trying to group the results so that they are grouped by category.

SearchResponse response = client.prepareSearch(\"search\")
                .addAggregation         


        
相关标签:
1条回答
  • 2021-01-27 17:21

    To apply elasticsearch mapping using java api,

    STEP 1) First create your mapping for the Elasticsearch type in a json file,

    eg. resources/Customer.json

    {
        "Customer" : {
                      "settings" : {
                      }, 
                      "properties"   : { 
                        "category" : { "type":"String" , "index" : "not_analyzed"}
                          } 
                       }
                  }
          }
    }
    

    STEP 2) create a java method to apply mapping from a json file, (see complete example here)

    class EsUtils {
    
      public static Client client
    
      public static void applyMapping(String index, String type, String location) throws Exception {
    
                String source = readJsonDefn(location);
    
                if (source != null) {
                    PutMappingRequestBuilder pmrb = client.admin().indices()
                                                          .preparePutMapping(index)
                                                          .setType(type);
                    pmrb.setSource(source);
                    MappingListener mappingListener = new MappingListener(pmrb)
    
                    // Create type and mapping
                    Thread thread = new Thread(mappingListener)
    
                    thread.start();
                    while (!mappingListener.processComplete.get()) {
                        System.out.println("not complete yet. Waiting for 100 ms")
                        Thread.sleep(100);
    
                    }
    
                } else {
                       System.out.println("mapping error");
                }
    
           }
    
           public static String readJsonDefn(String url) throws Exception {
                     //implement it the way you like 
                  StringBuffer bufferJSON = new StringBuffer();
    
                  FileInputStream input = new FileInputStream(new File(url).absolutePath);
                  DataInputStream inputStream = new DataInputStream(input);
                  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    
                  String line;
    
                  while ((line = br.readLine()) != null) {
                                 bufferJSON.append(line);
                  }
                  br.close();
                  return bufferJSON.toString();
           }
        }
    

    STEP 3) call applyMapping() method passing your es client,

    String index = "search"; //yourIndex
    String type  = "Customer";
    String location = "resources/Customer.json";
    
    EsUtils.client = yourClient; //pass your client
    EsUtils.applyMapping(index, type, location);
    

    STEP 4) query as you want,

    SearchRequestBuilder builder = client.prepareSearch("search");
    builder.addAggregation(AggregationBuilders.terms("categoryterms")
                                              .field("category").size(0))
    SearchResponse response = builder.execute().actionGet();
    

    Complete Reference

    Elasticsearch apply mapping

    0 讨论(0)
提交回复
热议问题