簡體   English   中英

Elasticsearch映射設置'not_analyzed'並按Java中的字段分組

[英]Elasticsearch mapping settings 'not_analyzed' and grouping by field in Java

我正在嘗試將結果分組,以便將它們按類別分組。

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

上面的代碼創建了聚合,但我遇到了一個問題,其中帶有連字符的字符串被分隔並放入了自己的“桶”中。

從我已閱讀的內容中,我需要更改映射設置,以便不分析類別,但是我不確定如何執行此操作。 寫入Elasticsearch或閱讀時完成了嗎? 究竟如何設置?

要使用Java API應用Elasticsearch映射,

步驟1)首先在json文件中為Elasticsearch類型創建映射,

例如。 resources/Customer.json

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

STEP 2)創建一個Java方法以應用json文件中的映射(請參見此處的完整示例)

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();
       }
    }

第3步)通過您的es客戶端調用applyMapping()方法,

String index = "search"; //yourIndex
String type  = "Customer";
String location = "resources/Customer.json";

EsUtils.client = yourClient; //pass your client
EsUtils.applyMapping(index, type, location);

步驟4)根據需要進行查詢

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

完整參考

Elasticsearch應用映射

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM