1

I am trying to find results by color. In the database, it is recorded in rgb format: an array of three numbers representing red, green, and blue values respectively.

Here is how it is stored in the db and elasticsearch record (storing 4 rgb colors in an array):

"color_data": 
    [
        [253, 253, 253], 
        [159, 159, 159], 
        [102, 102, 102], 
        [21, 21, 21]
    ]

Is there a query strategy that will allow me to find similar colors? i.e. exact match or within a close range of rgb values?

Here is a method I am trying, but the addressing method to access array values doesn't work:

curl -X GET 'http://localhost:9200/_search' -d '{
    "from": 0,
    "size": 50,
    "range": {
        "color_data.0.0": {
            "gte": "#{b_lo}",
            "lte": "#{b_hi}"
        },
        "color_data.0.1": {
            "gte": "#{g_lo}",
            "lte": "#{g_hi}"
        }
    }
}'

(r_lo, r_hi, et. al. are set to +/- 10 from the rgb values recorded in the color_data variable)

1 Answer 1

1
  • First, you should move channel data to separate fields (or to object field at least)

  • If you need simple matching algo (±deviation without scoring), then you can perform simple filter>range queries, passing your fuzziness threshold in query.

  • If you need scoring (how much similar that docs are), than you need to perform scripted queries. Take a look at this article

Btw, I strongly recommend work in HSL space, if you need such operations, you'll get much better results. Take a look at this example

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

1 Comment

I implemented this as a scripted search query per the example, but for some reason it is always returning the full record set...as if I queried match_all: {}. Not sure why this is happening...

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.