微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Elasticsearch映射设置'not_analyzed'并按Java中的字段分组

如何解决Elasticsearch映射设置'not_analyzed'并按Java中的字段分组

要使用Java API应用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();
       }
    }

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

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

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

完整参考

Elasticsearch应用映射

解决方法

我正在尝试将结果分组,以便将它们按类别分组。

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

上面的代码创建了聚合,但是我遇到了一个问题,其中 带有连字符的字符串 被分隔并放入了自己的“ Bucket”中。

从我已阅读的内容中,我需要更改映射设置,以便不分析类别,但是我不确定如何执行此操作。写入Elasticsearch或阅读时完成了吗?究竟如何设置?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。