# Help with a slightly complex query

**URL:** <https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085>\
**Category:** Elasticsearch\
**Created:** [September 20, 2012, 9:50pm UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085 "2012-09-20T21:50:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOutlander\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theoutlander_2/32/2614_2.png) [@TheOutlander\_2](https://discuss.elastic.co/u/TheOutlander_2)\
**Post date:** [September 20, 2012, 9:50pm UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085/1 "2012-09-20T21:50:37Z")

</div>

We have a JSON document where an attribute ("Names") is an array of  
elements in random order and another attribute ("Values") is an array where  
the order of elements corresponds to the order of elements in "Names".  
To simplify, think about two Arrays (Names and Values), where each index in  
Names corresponds to that index in values.

For example:

{  
"Names" : ["A", "B", "X", "C"],  
"Values" : [  
["Values1 for A", "Value 2 for A"],  
["Values1 for B", "Value 2 for B"],  
["Values1 for X", "Value 2 for X", "Value 3 for X"],  
["Values1 for C", "Value 2 for C"]  
]  
}

The user wants to query something like "Give me the value for 'X'" which in  
the above example should return a collection (["Values1 for X", "Value 2  
for X", "Value 3 for X"]).

In code, it's something like -\> return Values [Names.IndexOf( "X" )];

Is it possible to write a single query that will find the index from one  
array and fetch the value from another in Elastic Search??

Thanks,.  
Nick

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 20, 2012, 11:19pm UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085/2 "2012-09-20T23:19:49Z")

</div>

I suggest that you index your fields as separates docs.  
One doc for À, one for B, one for C and one for X.

It seems that you are searching for single content. I suggest to index what you are looking for.

I hope that my answer makes sense and fit with your use case.  
Or can you elaborate a bit more your case with a more concrete example?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 20 sept. 2012 à 23:50, TheOutlander [theoutlander@gmail.com](mailto:theoutlander@gmail.com) a écrit :

We have a JSON document where an attribute ("Names") is an array of elements in random order and another attribute ("Values") is an array where the order of elements corresponds to the order of elements in "Names".  
To simplify, think about two Arrays (Names and Values), where each index in Names corresponds to that index in values.

For example:

{  
"Names" : ["A", "B", "X", "C"],  
"Values" : [  
["Values1 for A", "Value 2 for A"],  
["Values1 for B", "Value 2 for B"],  
["Values1 for X", "Value 2 for X", "Value 3 for X"],  
["Values1 for C", "Value 2 for C"]  
]  
}

The user wants to query something like "Give me the value for 'X'" which in the above example should return a collection (["Values1 for X", "Value 2 for X", "Value 3 for X"]).

In code, it's something like -\> return Values [Names.IndexOf( "X" )];

Is it possible to write a single query that will find the index from one array and fetch the value from another in Elastic Search??

## Thanks,. Nick

--

---

<div class="post-metadata">

**Author:** ![sujoysett](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sujoysett](https://discuss.elastic.co/u/sujoysett)\
**Post date:** [September 21, 2012, 1:35pm UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085/3 "2012-09-21T13:35:12Z")

</div>

Hi,

Is remodeling the data a possible option? If it is, you could model it like  
the following :

{  
"Names": [  
"A",  
"B",  
"X",  
"C"  
],  
"Values": {  
"A": [  
"Values1 for A",  
"Value 2 for A"  
],  
"B": [  
"Values1 for B",  
"Value 2 for B"  
],  
"X": [  
"Values1 for X",  
"Value 2 for X",  
"Value 3 for X"  
],  
"C": [  
"Values1 for C",  
"Value 2 for C"  
]  
}  
}

and consequently use a query like the following to get the particular field

{  
"fields": [  
"Values.X"  
],  
"query": {  
"match\_all": {}  
}  
}

-- Sujoy.

On Friday, September 21, 2012 3:20:37 AM UTC+5:30, TheOutlander wrote:

> We have a JSON document where an attribute ("Names") is an array of  
> elements in random order and another attribute ("Values") is an array where  
> the order of elements corresponds to the order of elements in "Names".  
> To simplify, think about two Arrays (Names and Values), where each index  
> in Names corresponds to that index in values.
> 
> For example:
> 
> {  
> "Names" : ["A", "B", "X", "C"],  
> "Values" : [  
> ["Values1 for A", "Value 2 for A"],  
> ["Values1 for B", "Value 2 for B"],  
> ["Values1 for X", "Value 2 for X", "Value 3 for X"],  
> ["Values1 for C", "Value 2 for C"]  
> ]  
> }
> 
> The user wants to query something like "Give me the value for 'X'" which  
> in the above example should return a collection (["Values1 for X", "Value 2  
> for X", "Value 3 for X"]).
> 
> In code, it's something like -\> return Values [Names.IndexOf( "X" )];
> 
> Is it possible to write a single query that will find the index from one  
> array and fetch the value from another in Elastic Search??
> 
> Thanks,.  
> Nick

--

---

<div class="post-metadata">

**Author:** ![TheOutlander\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theoutlander_2/32/2614_2.png) [@TheOutlander\_2](https://discuss.elastic.co/u/TheOutlander_2)\
**Post date:** [September 21, 2012, 6:16pm UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085/4 "2012-09-21T18:16:57Z")

</div>

Sujoy - Thanks for the suggestion! It's an interesting approach.

I can't remodel existing data because that's the format they expect it in  
when they search. However, I might be able to do some pre-processing of the  
data on an end-point before inserting it and return the source doc. I'll  
give it some thought.

Thanks,  
Nick

On Friday, September 21, 2012 6:35:12 AM UTC-7, Sujoy Sett wrote:

> Hi,
> 
> Is remodeling the data a possible option? If it is, you could model it  
> like the following :
> 
> {  
> "Names": [  
> "A",  
> "B",  
> "X",  
> "C"  
> ],  
> "Values": {  
> "A": [  
> "Values1 for A",  
> "Value 2 for A"  
> ],  
> "B": [  
> "Values1 for B",  
> "Value 2 for B"  
> ],  
> "X": [  
> "Values1 for X",  
> "Value 2 for X",  
> "Value 3 for X"  
> ],  
> "C": [  
> "Values1 for C",  
> "Value 2 for C"  
> ]  
> }  
> }
> 
> and consequently use a query like the following to get the particular field
> 
> {  
> "fields": [  
> "Values.X"  
> ],  
> "query": {  
> "match\_all": {}  
> }  
> }
> 
> -- Sujoy.
> 
> On Friday, September 21, 2012 3:20:37 AM UTC+5:30, TheOutlander wrote:
> 
> > We have a JSON document where an attribute ("Names") is an array of  
> > elements in random order and another attribute ("Values") is an array where  
> > the order of elements corresponds to the order of elements in "Names".  
> > To simplify, think about two Arrays (Names and Values), where each index  
> > in Names corresponds to that index in values.
> > 
> > For example:
> > 
> > {  
> > "Names" : ["A", "B", "X", "C"],  
> > "Values" : [  
> > ["Values1 for A", "Value 2 for A"],  
> > ["Values1 for B", "Value 2 for B"],  
> > ["Values1 for X", "Value 2 for X", "Value 3 for X"],  
> > ["Values1 for C", "Value 2 for C"]  
> > ]  
> > }
> > 
> > The user wants to query something like "Give me the value for 'X'" which  
> > in the above example should return a collection (["Values1 for X", "Value 2  
> > for X", "Value 3 for X"]).
> > 
> > In code, it's something like -\> return Values [Names.IndexOf( "X" )];
> > 
> > Is it possible to write a single query that will find the index from one  
> > array and fetch the value from another in Elastic Search??
> > 
> > Thanks,.  
> > Nick

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:11am UTC](https://discuss.elastic.co/t/help-with-a-slightly-complex-query/9085/5 "2017-07-06T03:11:53Z")

</div>


