如何在搜索数组字段中推送对象数据

2024-04-19 14:48:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把对象数据类型推到一个数组字段中,我有一个错误,我要做的是映射

我使用Python来处理搜索

为客户创建映射

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', 'failed to parse [shopping_cart]')


Tags: iniduidindexifestypecustomer
1条回答
网友
1楼 · 发布于 2024-04-19 14:48:16

从您发布的映射来看,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端脚本来更新文档)。你知道吗

希望有帮助。你知道吗

相关问题 更多 >