Elasticsearch。 数据更新和保留以前字段值的历史记录

我的部分映射是:</ p>

 “current_price”=  &GT;  [“type”=&gt;  “float”],
“price_history”=&gt; [
“type”=&gt; “嵌套”,
“properties”=&gt; [
“date”=&gt; [“type”=&gt; “date”],
“value”=&gt; [“type”=&gt; “浮动”]
]
</ code> </ pre>

正如您所看到的,我保留了货物的当前存储价格以及之前的所有值。 我首先要注意的是,当我第一次创造商品时,我当然没有历史。 这就是为什么当我创建商品时,我根本不使用price_history,尽管它存在于我的映射中。</ p>

  $ params = [
'index'=&gt; config('storesettings.esIndex'),
'type'=&gt; config('storesettings.esType'),
'id'=&gt; $ id,
'body'=&gt; [

...
“current_price”=&gt; $ request-&gt; get('current_price'),
...

]
];
</ code> </ pre>

当我编辑商品时,我改变了 价格。 在这种情况下,我需要将当前价格移动到存档,将其移至price_history字段。 然后我需要替换当前名称。 问题是关于price_history字段。 我得到以前的值($ goods ['_ source'] ['price_history'])然后我添加到这个数组current_name。 我已经有了一些历史,一切都很好。 但如果我没有,那么我得到错误'Undefined index:price_history'。 在这种情况下,我应该检查:if(isset($ goods ['_ source'] ['price_history']))。 这是正常的吗? 在关系数据库中,我将有一个空数组,但在Elasticsearch中我没有,我必须做数组级别(可以这么说)检查。 如何处理此类案件? Maby我在创建商品时应该为price_history添加一个epmty数组吗?.. </ p>
</ div>

展开原文

原文

Part of my mapping is:

"current_price"         => ["type" => "float"],
"price_history"         => [
    "type"       => "nested",
    "properties" => [
    "date"  => ["type" => "date"],
    "value" => ["type" => "float"]
]

As you can see I keep in storage current price of goods and all the previous values. First thing I would like to notice is when I create goods in a very first time, I have no history, of course. That's why when I create goods, I do not use price_history at all, although it exists in my mapping.

$params = [
    'index' => config('storesettings.esIndex'),
    'type'  => config('storesettings.esType'),
    'id'    => $id,
    'body'  => [

        ... 
        "current_price"         => $request->get('current_price'),
        ...

    ]
];

When I edit goods, I change the price. In this case I need to move the current price to archive, moving it to price_history field. And then I need to replace current name. The question is about price_history field. I get previous value ($goods['_source']['price_history']) then I add to this array current_name. Everything is fine when I already have some history. But if I have not, then I get the error 'Undefined index: price_history'. In this case I should do checking: if(isset($goods['_source']['price_history'])). Is it normal? In relational databases I would have an empty array, but in Elasticsearch I haven't and I must do array level (so to speak) checking. How to handle such cases? Maby I should add an epmty array to price_history when I create goods?..

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问