gpt4 book ai didi

php - Elasticsearch PHP 添加路由到子文档

转载 作者:行者123 更新时间:2023-12-02 22:34:10 25 4
gpt4 key购买 nike

数据库服务器:
Elasticsearch 7.9.2
Centos 7.7
开发环境:
PHP 7.3.11
苹果系统
我对 Elasticsearch 还很陌生,所以请与我一起讨论这个问题。
不过,这让我发疯。
我正在尝试做一些非常简单的事情,但是由于我来自关系数据库世界,所以我需要一些思考。我创建了一个具有父子关系的映射。

Product --> Price
这是我创建的映射:
PUT /products_pc
{
"mappings": {
"properties": {
"datafeed_id": {
"type": "integer"
},
"date_add": {
"type": "date"
},
"description": {
"type": "text"
},
"ean": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"image_url": {
"type": "text",
"index": false
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sku": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"webshop_id": {
"type": "integer"
},
"price": {
"type": "float"
},
"url": {
"type": "text"
},
"date_mod":{
"type": "date"
},
"product_price" : {
"type":"join",
"relations": {
"product":"price"
}
}
}
}
}
到现在为止还挺好。当我手动添加产品和 2 个价格时,我可以得到我所期望的:1 个父级和 2 个子级文档。
现在到 PHP,我可以索引父文档,但不能索引子文档。看起来我无法发送路由参数(我可以使用 Kibana)
这是我在 PHP 中尝试过的,父 _id = 123
$hosts = ['xxx.xxx.xxx.xxx:9200'];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();

$params['body'][] = [
'create' => [
'_index' => 'products_pc',
'_id' => '123_1'
]
];
$params['body'][] = [
'webshop_id' => 1,
'date_mod' => time(),
'price' => 12,
'url' => '',
'product_price' => [
'name' => 'price',
'parent' => 123
]
];
$client->bulk($params);
但这不起作用,因为没有路由集。如果我在 _id 字段下方添加 '_routing' => 123,则会收到 400 错误,告诉我 _routing 字段错误(“操作/元数据行 [3] 包含未知参数 [_routing]”)
我一直在寻找2天,在圈子里跑。所有不同的 Elasticsearch 版本都略有不同,所以我不得不承认我迷路了。有没有人可以指出我的错误?还是正确方向的提示?这让我快疯了。 (因为我担心它会太简单了......)
提前致谢!

最佳答案

所以我们在这里,经过 2 多天的搜索......但我找到了似乎解决方案......
经过几个小时的搜索,我最终来到了这个页面(再次):
https://elastic.co/guide/en/elasticsearch/client/php-api/current/ElasticsearchPHP_Endpoints.html#Elasticsearch_Clientbulk_bulk
在批量端点的参数列表中:

$params['routing']                =  // (string) Specific routing value
一开始不太确定如何使用它,但是...
然后我对每个子文档都尝试了这个,这似乎在起作用!
$hosts = ['xxx.xxx.xxx.xxx:9200'];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();

// insert price
$params['body'][] = [
'index' => [
'_index' => 'products_pc',
'_id' => '123_1',
'routing' => 123 // <-- Insert routing here.
]
];
$params['body'][] = [
'webshop_id' => 1,
'date_mod' => time(),
'price' => 12,
'url' => '',
'product_price' => [
'name' => 'price',
'parent' => 123 // <-- Parent _id value
]
];

$client->bulk($params);
和之前想的一样,其实太容易了。但我想这就是程序员的生活。
但请注意,很多文档都提到了 _routing 字段(甚至 7.9 版的官方文档: https://www.elastic.co/guide/en/elasticsearch/reference/7.9/mapping-routing-field.html 如文本中元数据字段下的右侧子菜单所示),但该字段实际上只是“路由”。可能会为您节省几天时间;-)

关于php - Elasticsearch PHP 添加路由到子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64507979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com