Q:非洲食人族的酋长吃什么?

求教怎么在Java里创建ElasticSearch的父子关系模型?

Elasticsearch | 作者 SindyLv | 发布于2018年05月11日 | 阅读数:2934

有两个数据库表product_detail(A)和product_online_date(B)表,是A对B是一对多的关系,想把这两个表数据存到ElasticSearch,构建父子关系。代码写的如下:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(
new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));

XContentBuilder relationMapping = JsonXContent.contentBuilder().startObject()
.startObject("mappings")
.startObject("product").endObject()
.startObject("online_date")
.startObject("_parent").field("type", "product").endObject()
.endObject().endObject().endObject();

PutMappingRequest pmr = Requests.putMappingRequest("products").source(relationMapping);
client.admin().indices().putMapping(pmr).actionGet();
 
报错信息如下:


{"mappings":{"product":{},"online_date":{"_parent":{"type":"product"}}}}
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;
at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:26)
at org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest.validate(PutMappingRequest.java:93)
at org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:46)
at org.elasticsearch.client.transport.TransportProxyClient.lambda$execute$0(TransportProxyClient.java:60)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:250)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:371)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1247)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.putMapping(AbstractClient.java:1494)
at com.mei.choose.elasticsearch.ElasticSearchService.main(ElasticSearchService.java:69)

 
求教大神,怎么才能正确建立父子关系模型
已邀请:

laoyang360 - Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: SindyLv

你先通过dsl建立映射,父子文档的关键是父文档和子文档通过一个核心字段关联,同时子文档数据导入的时候都关联父文档id。
核心官网参考:https://elasticsearch.cn/book/ ... .html

6.x版本参考:https://blog.csdn.net/laoyang3 ... 74481

yan19940607

赞同来自:

这个是我自己刚学es写的插入文档的方法,你可以看看
/** 
* 这个方法是创建文档的 将文档添加到服务器 并且可以被搜索 
*@param indexName 索引名称 
*@param indexType 索引类型 
*@param docId 文档id 
*@param parent 父子关系中的父节点 
* @param objMap 字段信息 
*/ 
public static void addDocument(String indexName, String indexType, String docId, String parent, Map<String, Object> objMap) 

       try { 
           XContentBuilder xContentBuilder = jsonBuilder() .startObject(); 
           for (Map.Entry<String, Object> entry : objMap.entrySet()) { 
                xContentBuilder.field(entry.getKey(), entry.getValue()); 
           } 
           xContentBuilder = xContentBuilder.endObject(); 
           IndexResponse response = client.prepareIndex(indexName, indexType, docId)
                           .setParent(parent) 
                           .setSource(xContentBuilder) 
                           .get(); 
           System.out.println(response.toString()); 
       } catch (Exception e) { 
           System.out.println(e.getStackTrace()); 
       } 
}
 

yan19940607

赞同来自:

创建父索引的mappings
{
  "mappings": {
    "student":{
    "properties":{
    "stu_name":{
    "type":"keyword"
    },
    "stu_birthday":{
    "type":"date",
    "format":"yyyy-MM-dd"
    }
    }
    }
  }
}
创建子索引的mappings

{
"mappings":{
"stu_school":{
"_parent":{
"type":"student"
},
"properties":{
"school_name": {
"type":"keyword"
},
"start_year": {
"type":"date",
"format":"yyyy-MM-dd"
},
"end_year": {
"type":"date",
"format":"yyyy-MM-dd"
},
"school_address" : {
"type":"text"
}
}
}
}
}
 

zwy_

赞同来自:

 
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject("properties")
.startObject("join_field")
.field("type","join") //关系
.startObject("relations")
.field("company","personPerformance")
.endObject()
.endObject()
.startObject("personPerformance") //子文档
.startObject("properties")
.startObject("personId").field("type","keyword").endObject()
.startObject("performances").field("type","nested")
.startObject("properties")
.startObject("type").field("type","keyword").endObject()
.startObject("aim").field("type","keyword").endObject()
.startObject("role").field("type","keyword").endObject()
.startObject("relationlink").field("type","keyword").endObject()
.startObject("allinvest").field("type","float").endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.startObject("company")//父文档
.startObject("properties")
.startObject("id").field("type","keyword").endObject()
.startObject("labels").field("type","nested")
.startObject("properties")
.startObject("color").field("type","keyword").endObject()
.startObject("name").field("type","keyword").endObject()
.startObject("type").field("type","keyword").endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();

zwy_

赞同来自:

上面那个可以建,但是查询的时候,不好处理,反正我没弄出来。
 
我按照
 https://www.bmc.com/blogs/elas ... rent/
 
这个地址重新弄了下,查询也可以了
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject("properties")
.startObject("id").field("type","keyword").endObject()

.startObject("labels").field("type","nested")
.startObject("properties")
.startObject("color").field("type","keyword").endObject()
.startObject("name").field("type","keyword").endObject()
.startObject("type").field("type","keyword").endObject()
.endObject()
.endObject()
.startObject("companyPersonRelation")
.field("type","join")
.startObject("relations")
.field("company","personPerformance")
.endObject()
.endObject()
.endObject()
.endObject();

要回复问题请先登录注册