Sort nested documents in search result


(Zdenek Pizl) #1

Hallo,

let's say I have following structure of document with nested (multiple)
documents :

{
"host": "server-001",
"plugins": [
{
"plugin_name": "function-c",
"plugin": {"function-c": "some C content" }
},
{
"plugin_name": "function-a",
"plugin": {"function-a": "some A content" }
},
{
"plugin_name": "function-b",
"plugin": {"function-b": "some B content" }
}
]
}

I would like to get result of search with sorted nested plugins' objects,
that is JSON with plugin_name item in 'function-a, function-b, function-c'
sequence of nested documents. Is it even possible to achieve? If it is, how
should the query and/or mapping look like?

I've tried i.e. '{ "query": { "match": { "host": "server-001" }}, "sort": {
"plugins.plugin_name" : "asc" }}}' but it does not sort the nested block at
all.

Thank you, regards Z. Pizl.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(David Pilato) #2

Have a look at nested sort in docs: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_nested_sorting_example

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

Le 9 juin 2014 à 00:12, Zdenek Pizl zdenek.pizl@gmail.com a écrit :

Hallo,

let's say I have following structure of document with nested (multiple) documents :

{
"host": "server-001",
"plugins": [
{
"plugin_name": "function-c",
"plugin": {"function-c": "some C content" }
},
{
"plugin_name": "function-a",
"plugin": {"function-a": "some A content" }
},
{
"plugin_name": "function-b",
"plugin": {"function-b": "some B content" }
}
]
}

I would like to get result of search with sorted nested plugins' objects, that is JSON with plugin_name item in 'function-a, function-b, function-c' sequence of nested documents. Is it even possible to achieve? If it is, how should the query and/or mapping look like?

I've tried i.e. '{ "query": { "match": { "host": "server-001" }}, "sort": { "plugins.plugin_name" : "asc" }}}' but it does not sort the nested block at all.

Thank you, regards Z. Pizl.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/F200833A-E80F-4EBA-B4D9-C83D75B42790%40pilato.fr.
For more options, visit https://groups.google.com/d/optout.


(Zdenek Pizl) #3

Hi David,

the example is known to me and probably it is not what I am looking for or
I am doing something dumb. I'll try to explain it in deeper details.

Let's say we have 2 documents within index /sort-test/ with following
mapping:

Creating an index and its mapping:

curl -XPUT http://localhost:9200/sort-test
curl -XPUT 'http://localhost:9200/sort-test/node/_mapping' -d ' { "node": {
"properties" : { "plugins" : { "type" : "nested"}, "host": { "index":
"not_analyzed", "type": "string" }}}}'

Documents:

curl -XPOST http://localhost:9200/sort-test/node/ -d '{

"host": "server-002",
"plugins": [
{ "plugin_name": "function-c", "plugin": {"function-c": "some C
content" }},
{ "plugin_name": "function-a", "plugin": {"function-a": "some A
content" }},
{ "plugin_name": "function-b", "plugin": {"function-b": "some B
content" }}
]
}'

curl -XPOST http://localhost:9200/sort-test/node/ -d '{

"host": "server-001",
"plugins": [
{ "plugin_name": "function-x", "plugin": {"function-x": "some X
content" }},
{ "plugin_name": "function-d", "plugin": {"function-d": "some D
content" }}
]
}'

And now I would like to get a document of host=server-001 in following form:

'{

"host": "server-001",
"plugins": [

  • { "plugin_name": "function-d", "plugin": {"function-d": "some D
    content" }}*
  • { "plugin_name": "function-x", "plugin": {"function-x": "some X
    content" }}, *
    ]
    }'

^^^ Note there are plugins's nested items sorted in ascending order
according to plugin_name. THAT is the result I need to achieve. I've tried
following:

curl -XPOST http://localhost:9200/sort-test/_search?pretty=1 -d'{ "query":
{ "match": { "host": "server-001" }}, "sort": { "plugins.plugin_name" :
"asc" }}}'
or
curl -XPOST http://localhost:9200/sort-test/_search?pretty=1 -d'{ "query":
{ "match": { "host": "server-001" }}, "sort": { "plugins.plugin_name" : {
"order": "asc", "nested_path": "plugins" } }}}'

with no luck.

The result is still the same, without sorted plugins items:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : null,
"hits" : [ {
"_index" : "sort-test",
"_type" : "node",
"_id" : "Zg41zMjwRYy77OU9vJ7YHQ",
"_score" : null, "_source" : {
"host": "server-001",
"plugins": [
{ "plugin_name": "function-x", "plugin": {"function-x": "some X
content" }},
{ "plugin_name": "function-d", "plugin": {"function-d": "some D
content" }}
]
},
"sort" : [ "d" ]
} ]
}
}

Any idea how to achieve this? Thank you in advance, regard .zp.

--

You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearc...@googlegroups.com <javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com
https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9d1e204e-6d2e-4d85-8c7b-bb7c7d608aed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4