Help with a slightly complex query

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

--

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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 20 sept. 2012 à 23:50, TheOutlander 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

--

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

--

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

--