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()
上面的代码创建了聚合,但我遇到一个问题,其中带有连字符的字符串他们被分离并放入自己的Bucket。
从我所看到的,我需要更改映射设置,以便该类别是没有分析,但我不知道如何做到这一点。在写信给Elasticsearch或阅读时,这样做吗?如何设置?
使用java api应用弹性搜索映射,
步骤1)首先在json文件中为Elasticsearch类型创建映射,
例如。 resources / Customer.json
{
客户 :{
settings:{
},
properties:{
category:{type:String,index:not_analyzed }
}
}
}
}
}
步骤2)创建一个java方法来应用来自json文件的映射(见完整的例子 here )
class EsUtils {
public static客户端客户端
public static void applyMapping(String index,String type,String location)throws异常{
String source = readJsonDefn(location) ;
if(source!= null){
PutMappingRequestBuilder pmrb = client.admin()。indexes()
.preparePutMapping(index)
.setType(type) ;
pmrb.setSource(source);
MappingListener mappingListener = new MappingListener(pmrb)
//创建类型和映射
线程线程=新线程(mappingListener)
thread.start( );
while(!mappingListener.processComplete.get()){
System.out.println(还没完成,等待100 ms)
Thread.sleep(100);
}
} else {
System.out.println(mapping error);
}
}
public static String readJsonDefn(String url)throws异常{
//按照你喜欢的方式实现
StringBuffer bufferJSON = new StringBuffer();
FileInputStream input = new FileInputStream(new File(url).absolutePath);
DataInputStream inputStream = new DataInputStream(input);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
字符串;
while((line = br.readLine())!= null){
bufferJSON.append(line);
}
br.close();
return bufferJSON.toString();
}
}
步骤3)调用applyMapping()方法传递您的客户端
String index =search; // yourIndex
String type =Customer;
String location =resources / Customer.json;
EsUtils.client = yourClient; //传递你的客户
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();
完整参考
I'm trying to group the results so that they are grouped by category.
SearchResponse response = client.prepareSearch("search")
.addAggregation(AggregationBuilders.terms("category").field("category").size(0))
.execute()
.actionGet();
The code above creates aggregations but I'm running into a problem where strings with hyphens in them are being separated and put into their own 'Bucket'.
From what I've read I need to change the mapping settings so that the category is not analysed but I'm not sure how to do this. Is this done when writing to Elasticsearch or when reading? How is it set exactly?
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映射设置'not_analyzed'和按字段分组在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!