【问题标题】:How to push object data in elasticksearch array field如何在elasticsearch数组字段中推送对象数据
【发布时间】:2017-11-08 06:47:09
【问题描述】:

我想在数组字段中推送对象数据类型,我有错误,我的事情是映射

我使用 Python 来处理 ElastickSearch

为客户创建映射

def create_customer_index():
    ''' Start creating customers index manually '''

    mapping = {
        "mappings": {
            "customer": {
                "properties": {
                    "created": {
                        "type": "long"
                    },
                    "updated": {
                        "type": "long"
                    },
                    "shopping_cart": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },

                }
            }
        }
    }

    es.indices.create(index='customers', body=mapping)
#end

通过这种方法,我添加了用户选择的产品和数组中的数量

def add_item_in_shopping_cart(uid, product_id, quantity=1):
    ''' Add new item in customer shopping cart '''

    print('Start adding a new item in shopping cart')

    # print('UID => ', uid)
    print('Product id to add is: => ', product_id)

    # 1st check if the user has this product on favorites,
    # 2nd if the user doesn't have this remove it from the list

    customer_shoping_cart = get_customer_shopping_cart(uid)
    print('CUSTUMER SHOPING CART => ', customer_shoping_cart)

    x = False

    for item in customer_shoping_cart:
        if item == product_id:
            x = True
    if x:
        print('Item already exist in shopping_cart')
        return 'item_already_exist_in_shopping_cart', 500
    else:
        print('Item dont exist in shopping_cart, i gona added')
        timestamp = int(round(time.time() * 1000))

        schema = {
            "product_id": product_id,
            "quantity" : 10
        }
        doc = {
            "script" : {
                "inline":"ctx._source.shopping_cart.add(params.data)",
                "params":{
                    "data":{
                        "product_id": product_id,
                        "quantity" : quantity
                    }
                }
            }
        }

        try:
            es.update(index="customers", doc_type='customer', id=uid, body=doc)
            es.indices.refresh(index="customers")
            return jsonify({'message': 'item_added_in_shopping_cart'}), 200

        except Exception as e:
            print('ERROR => ', e)
            return 'we have error', 500
#end

我有这个错误

ERROR => TransportError(400, 'mapper_parsing_exception', '无法解析 [shopping_cart]')

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    从您发布的映射来看,ElasticSearch 需要这样的文档:

    {
      "created": 1510094303,
      "updated": 1510094303,
      "shopping_cart": "I am in a shopping cart"
    }
    

    或者像这样:

    {
      "created": 1510094303,
      "updated": 1510094303,
      "shopping_cart": [
        "I am in a shopping cart",
        "me too!"
      ]
    }
    

    而您正试图将"shopping_cart" 视为一个对象数组,而事实并非如此(它是一个字符串数组)。 ElasticSearch 不允许将不适合映射的对象放入索引中。

    您应该首先尝试将映射更改为以下内容:

    mapping = {
        "mappings": {
            "customer": {
                "properties": {
                    "created": {
                        "type": "long"
                    },
                    "updated": {
                        "type": "long"
                    },
                    "shopping_cart": {
                        "properties": {
                            "product_id": {
                                "type": "keyword"
                            },
                            "quantity": {
                                "type": "integer"
                            }
                        }
                    },
    
                }
            }
        }
    }
    

    此外,还要考虑完全在客户端更改文档,即在您的脚本中,并将新版本的文档放入索引中(替换以前的),因为在实现逻辑中可能会更容易(例如,不需要 ElasticSearch 端脚本来更新文档)。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 2018-10-28
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多