# Any tips? Span\_near with nested documents

**URL:** <https://discuss.elastic.co/t/any-tips-span-near-with-nested-documents/96678>\
**Category:** Elasticsearch\
**Created:** [August 10, 2017, 9:50pm UTC](https://discuss.elastic.co/t/any-tips-span-near-with-nested-documents/96678 "2017-08-10T21:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustyob](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@dustyob](https://discuss.elastic.co/u/dustyob)\
**Post date:** [August 10, 2017, 9:50pm UTC](https://discuss.elastic.co/t/any-tips-span-near-with-nested-documents/96678/1 "2017-08-10T21:50:16Z")

</div>

Hi, I've tried searching, to no avail.

What I want to do is to find documents that contain nested documents, each with different properties, in-order.

E.g. I would like to find documents with nestedthings.myid=nested1 coming before nestedthings.myid=nested2, in the example below this would be document c1 but NOT c2.

NOTE: Eventually the nested documents will be complex documents. Each with perhaps a different schema. But for now they're trivially simple.

PUT span-test/container/c1  
{  
"myid": "c1",  
"nestedthings": [  
{  
"myid": "nested1"  
},  
{  
"myid": "nested2"  
}  
]  
}

PUT span-test/container/c2  
{  
"myid": "c2",  
"nestedthings": [  
{  
"myid": "nested2"  
},  
{  
"myid": "nested1"  
}  
]  
}

My understanding is that I want to use a span\_near query to accomplish this the most efficiently. But I'm not wed to any particular approach.

NOTE: I'm using nested documents because they exist independently and eventually I'll have multiple properties under each and I want each nested-doc to act as a coherent whole. Anyways with span\_near, all of the examples are processing single strings. I can't really serialize my nested documents into strings because of their eventual complexity.

Please help.

I'll reply here with index etc.

---

<div class="post-metadata">

**Author:** ![dustyob](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@dustyob](https://discuss.elastic.co/u/dustyob)\
**Post date:** [August 10, 2017, 9:55pm UTC](https://discuss.elastic.co/t/any-tips-span-near-with-nested-documents/96678/2 "2017-08-10T21:55:56Z")

</div>

**INDEX**

PUT span-test  
{  
"settings": {  
"number\_of\_shards": 1,  
"number\_of\_replicas": 0  
},  
"mappings": {  
"container": {  
"properties": {  
"myid": {  
"type": "string",  
"index": "not\_analyzed",  
"index\_options": "positions"  
},  
"nestedthings": {  
"type": "nested",  
"properties": {  
"myid": {  
"type": "string",  
"index": "not\_analyzed",  
"index\_options": "positions"  
}  
}  
}  
},  
"\_all": {  
"enabled": true,  
"index": "not\_analyzed",  
"analyzer": "keyword"  
}  
}  
}  
}

**DATA**

PUT span-test/container/c1  
{  
"myid": "c1",  
"nestedthings": [  
{  
"myid": "nested1"  
},  
{  
"myid": "nested2"  
}  
]  
}

PUT span-test/container/c2  
{  
"myid": "c2",  
"nestedthings": [  
{  
"myid": "nested2"  
},  
{  
"myid": "nested1"  
}  
]  
}

**BASIC QUERIES**

GET span-test/container/\_search  
{  
"query": {"match\_all": {}}  
}  
**^^^^^ WORKS**

GET span-test/container/\_search  
{  
"query": {  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested1"  
}  
}  
}  
}  
}  
**^^^^^ WORKS**

GET span-test/container/\_search  
{  
"query": {  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested2"  
}  
}  
}  
}  
}  
**^^^^^ WORKS**

GET span-test/container/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested1"  
}  
}  
}  
},  
{  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested2"  
}  
}  
}  
}  
]  
}  
}  
}  
**^^^^^ KIND OF WORKS BUT RETURNS c2 (and c1) when I only want c1 (because of order)**

GET span-test/container/\_search  
{  
"query": {  
"span\_near": {  
"clauses": [  
{  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested1"  
}  
}  
}  
},  
{  
"nested": {  
"path": "nestedthings",  
"query": {  
"span\_term": {  
"nestedthings.myid": "nested2"  
}  
}  
}  
}  
],  
"slop": 12,  
"in\_order": false  
}  
}  
}  
**^^^^ DOESN'T WORK -- GIVES ERROR:**  
"error": {  
"root\_cause": [  
{  
"type": "query\_parsing\_exception",  
"reason": "[nested] nested object under path [nestedthings] is not of nested type",  
"index": "span-test",  
"line": 4,  
"col": 15  
}  
],

Reading the documentation, I get it that this isn't syntactically allowed. And so, I've tried various workarounds like wrapping the nested with span\_multi, etc, taking me in some really weird places. Nothing really works.

Anyways, the above should illustrate what I really want to do.

Thanks!  
Dusty

---

<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:** [September 7, 2017, 9:56pm UTC](https://discuss.elastic.co/t/any-tips-span-near-with-nested-documents/96678/3 "2017-09-07T21:56:30Z")

</div>

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