栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

要使用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"; //yourIndexString type  = "Customer";String location = "resources/Customer.json";EsUtils.client = yourClient; //pass your clientEsUtils.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应用映射



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号