elasticsearch php向子文档添加路由

wlzqhblo  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(292)

数据库服务器:elasticsearch 7.9.2 centos 7.7
开发环境:PHP7.3.11 macos
我对elasticsearch还比较陌生,所以请跟我说说这个。不过,这让我快疯了。
我试图做一些非常简单的事情,但由于我来自关系数据库世界,我需要一些头脑弯曲。我创建了一个具有父子关系的Map。

Product --> Price

这是我创建的Map:

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"
          }
        }
    }
  }
}

到现在为止,一直都还不错。当我手动添加一个产品和两个价格时,我可以得到我所期望的:一个父文档和两个子文档。
现在转到php,我可以索引父文档,但不能索引子文档。看起来我无法发送路由参数(我可以使用kibana)
这是我在php中尝试的,parent\u 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字段下面添加“\u routing”=>123,我会得到一个400错误,告诉我“路由”字段是错误的(“操作/元数据行[3]包含一个未知参数[\u routing]”)
我已经找了两天了,跑来跑去。所有不同的elasticsearch版本都略有不同,所以我不得不承认我迷路了。有人能指出我的错误吗?或者是正确方向的暗示?快把我逼疯了(因为我担心这太简单了……)
提前谢谢!

dy2hfwbg

dy2hfwbg1#

所以我们在这里,经过两天的搜索。。。但我已经找到了解决办法。。。
再搜索几个小时后,我又回到了这个页面: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);

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

相关问题