【问题标题】:Elasticsearch PHP add routing to child documentElasticsearch PHP 添加路由到子文档
【发布时间】:2020-10-28 19:23:48
【问题描述】:

数据库服务器: 弹性搜索 7.9.2 Centos 7.7

开发环境: PHP 7.3.11 macOS

我对 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 版本都略有不同,所以我不得不承认我迷路了。有没有人可以指出我的错误?还是正确方向的提示?这让我快疯了。 (恐怕做起来太简单了……

提前致谢!

【问题讨论】:

    标签: php elasticsearch routes parent-child


    【解决方案1】:

    所以我们在这里,经过 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 如文本中元数据字段下的右侧子菜单中所示),但该字段实际上只是“路由”。可能会为您节省几天时间;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      相关资源
      最近更新 更多