Filter an array of objects to return only matching values

(Paddy Mahadeva) #1


I have a set of data which are simply key value pairs, of type string.

          key : Name1, Value : Value1 
          key : Name2, Value : Value2 
          key : Name3, Value : Value3
          key : Name3, Value : Value3
          key : Name100000, Value : Value100000

The data set is almost over 1000 records, I know that these are always going to be of type keyword, Is there any way I can maintain all this data in a single document and fetch the value for a given key.

ie . I want to fetch the value for Key = Name2 , and I want only its value. Is there anyway to write a query to get this data?


(David Pilato) #2

Instead of sending something like:

POST foo/_doc
  "values": [
    { "key": "Name1", "value" : "Value1" },
    { "key": "Name2", "value" : "Value2" },
    { "key": "Name3", "value" : "Value3" }

Why not indexing individual documents like:

POST foo/_doc
{ "key": "Name1", "value" : "Value1" }
POST foo/_doc
{ "key": "Name2", "value" : "Value2" }
POST foo/_doc
{ "key": "Name3", "value" : "Value3" }

Then that will be obvious...

(Paddy Mahadeva) #3

Just for 2 fields why should I create 1000 documents? Rather I can have this in a heterogeneous index that handles a lot of metadata for my application, and this could be one such document if the array filtering logic works out.

Here is my use case.

Lets say every time my application processes a particular word I need it to be replaced with another word, and there could be thousands of such words. What is the real value creation here? Maintaining so much jsons that I created in my code base is going to be a nightmare.

Where as If I can execute it as a single json with an array of such objects. I dont have to worry about maintaining these documents, and anyway the data is getting indexed whether I am storing it as nested or flat. So is there a solution to handle this?

(David Pilato) #4

Just for 2 fields why should I create 1000 documents?

As you prefer.

I personally always prefer indexing what I'm searching for than something else.
Like if I want to search for an object, I'll index an object and not an array of objects. Because what I want to get back is an object, nothing else.

Anyway, have a look at
That might help you.

(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.