1

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?

2
  • run each product through an array_filter before indexing them ElasticSearch doesn't index empty arrays anyway. Commented Oct 5, 2018 at 15:24
  • In Elasticsearch every field stores an array. So, type of code, properties and attributes all should be string not array. (there is no array type) Commented Oct 6, 2018 at 17:19

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.