有两个数据库表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)
求教大神,怎么才能正确建立父子关系模型
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)
求教大神,怎么才能正确建立父子关系模型
5 个回复
laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net
赞同来自: SindyLv
核心官网参考:https://elasticsearch.cn/book/ ... .html
6.x版本参考:https://blog.csdn.net/laoyang3 ... 74481
yan19940607
赞同来自:
/**
* 这个方法是创建文档的 将文档添加到服务器 并且可以被搜索
*@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": {
"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_
赞同来自:
zwy_
赞同来自:
我按照
https://www.bmc.com/blogs/elas ... rent/
这个地址重新弄了下,查询也可以了