kinger198
kinger198

Reputation: 647

ElasticSearch indexing and mapping arrays

I'm currently building a search for an ecommerce system using ElasticSearch (PHP client) but I've run in to an issue

The data I'm storing is a mixture of simple text (e.g. product titles) and lists (e.g. colours, sizes)

The issue I've ran in to is when a product doesn't have any colours

I have the following fields

id - integer
title - string
codes - array
properties - array
attributes - array

So when I send up a product, it looks like this

id: 1
title: ABC
codes: ['ABC', '123']
properties: ['purple']
attributes: ['large', 'small']

This works as expected, but when I try and send something like this

id: 2
title: DEF
codes: ['DEF']
properties: []
attributes: []

It throws an error:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [codes] tried to parse field [null] as object, but found a concrete value"}],"type":"mapper_parsing_exception","reason":"object mapping for [codes] tried to parse field [null] as o bject, but found a concrete value"},"status":400}

I've tried mapping my fields using this for each field (as found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html)

attributes: {
    dynamic: true,
    properties: []
}

But that doesn't seem to help, and the docs say you can't specify a type of array

Can anyone point me in the right direction?

Upvotes: 1

Views: 1345

Answers (1)

kinger198
kinger198

Reputation: 647

Thanks to apokryfos and Amir masud zarebidaki for the comments, combined they got me to the answer

I ended up sending the mapping for codes, attributes and properties as 'text', and by default the object is created as dynamic so they can have multiple values

I also made my code strip out any empty arrays so if a product has no properties, they won't be sent

These two changes combined have solved my error

A heads up in case anyone else gets this error:

Can't get text on a START_OBJECT

Check you're not sending up an array with non-incremental keys, for example:

[attributes] => Array
(
    [0] => Game
    [1] => Chicken
    [2] => Salmon
    [6] => Chicken
)

I ran in to this issue, a simple array_values($attributes) fixed this one for me (PHP)

Upvotes: 1

Related Questions