# A couple of span query questions

**URL:** <https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525>\
**Category:** Elasticsearch\
**Created:** [September 9, 2013, 4:34pm UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525 "2013-09-09T16:34:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curt\_Kohler](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@Curt\_Kohler](https://discuss.elastic.co/u/Curt_Kohler)\
**Post date:** [September 9, 2013, 4:34pm UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525/1 "2013-09-09T16:34:15Z")

</div>

I'm in the process of trying to understand span queries and am having  
running into so problems that I can't find an answer for in the  
documentation. I was hoping someone on the group could shed some light.

I have loaded up some documents that have analyzed textual fields and I  
have a requirement to support proximity based queries in addition to normal  
textual queries. These documents are presented to a user in a search form  
with searchable fields that can map to multiple index fields. E.g search  
form fieldA maps to field1 and field2 in the index. An example  
proximity search might be fieldA(Pain PRE/6 Morphine). Since order is  
important, that leads me down the road to a SPAN\_NEAR query with "in\_order"  
set to true. Since term queries don't analyze the text, I need to do that  
prior to sending the search down (e.g. Morphine would need to be morphin  
with the analyzers we are using on those fields). Doing a single span\_near  
query on one of the two fields works, but when I try to combine them with a  
span\_or query as below I get a parse exception

{  
"query": {  
"span\_or": {  
"clauses": [  
{  
"span\_near": {  
"clauses": [  
{  
"span\_term": {  
"field1": "pain"  
}  
},  
{  
"span\_term": {  
"field1": "morphin"  
}  
}  
],  
"slop": 6,  
"in\_order": true  
}  
},  
{  
"span\_near": {  
"clauses": [  
{  
"span\_term": {  
"field2": "pain"  
}  
},  
{  
"span\_term": {  
"field2": "morphin"  
}  
}  
],  
"slop": 6,  
"in\_order": true  
}  
}  
]  
}  
}  
}

Gives  
{

- error: SearchPhaseExecutionException[Failed to execute phase [query],  
total failure; shardFailures {[XrFytwobQPOTn2QOInx8jg][hothouse][3]:  
SearchParseException[[hothouse][3]: from[-1],size[-1]: Parse Failure  
[Failed to parse source  
[{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
nested: IllegalArgumentException[Clauses must have same field.];  
}{[XrFytwobQPOTn2QOInx8jg][hothouse][1]:  
SearchParseException[[hothouse][1]: from[-1],size[-1]: Parse Failure  
[Failed to parse source  
[{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
nested: IllegalArgumentException[Clauses must have same field.]; }]
- status: 500

}

The documentation states the the clauses to a span\_or (or any of the span  
queries ) must be span\_type queries. I assume there is more to it than  
this based on the above error?

I also have a follow up question to the above... If I swap out the span\_or  
w/ clauses for a boolean/should structure, the request above runs (although  
I will have issues as I start having to support more complex proximity  
queries taking this approach) but I don't get any highlighting back for  
the request.. However, if I run the the request using match query of type  
phrase with the slop of 6 I do get highlight results. The highlight clause  
of the query structure is the same between the two approaches. Is anyone  
aware of highlighting issues with span queries?

Thanks for any insights...

Curt

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 9, 2013, 4:47pm UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525/2 "2013-09-09T16:47:45Z")

</div>

Hi,  
the error that you get back is because all clauses must be related to the  
same field, beyond being span\_term queries. In your case you are referring  
to both field1 and field2. Span queries can be complex but always need to  
stay on the same field.

There might be issues while highlighting when using span queries in lucene,  
depending on the highlighter type you're using. Plain highlighter of fast  
vector highlighter?

On Monday, September 9, 2013 6:34:15 PM UTC+2, Curt Kohler wrote:

> I'm in the process of trying to understand span queries and am having  
> running into so problems that I can't find an answer for in the  
> documentation. I was hoping someone on the group could shed some light.
> 
> I have loaded up some documents that have analyzed textual fields and I  
> have a requirement to support proximity based queries in addition to normal  
> textual queries. These documents are presented to a user in a search form  
> with searchable fields that can map to multiple index fields. E.g search  
> form fieldA maps to field1 and field2 in the index. An example  
> proximity search might be fieldA(Pain PRE/6 Morphine). Since order is  
> important, that leads me down the road to a SPAN\_NEAR query with "in\_order"  
> set to true. Since term queries don't analyze the text, I need to do that  
> prior to sending the search down (e.g. Morphine would need to be morphin  
> with the analyzers we are using on those fields). Doing a single span\_near  
> query on one of the two fields works, but when I try to combine them with a  
> span\_or query as below I get a parse exception
> 
> {  
> "query": {  
> "span\_or": {  
> "clauses": [  
> {  
> "span\_near": {  
> "clauses": [  
> {  
> "span\_term": {  
> "field1": "pain"  
> }  
> },  
> {  
> "span\_term": {  
> "field1": "morphin"  
> }  
> }  
> ],  
> "slop": 6,  
> "in\_order": true  
> }  
> },  
> {  
> "span\_near": {  
> "clauses": [  
> {  
> "span\_term": {  
> "field2": "pain"  
> }  
> },  
> {  
> "span\_term": {  
> "field2": "morphin"  
> }  
> }  
> ],  
> "slop": 6,  
> "in\_order": true  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> Gives  
> {
> 
> - error: SearchPhaseExecutionException[Failed to execute phase  
> [query], total failure; shardFailures  
> {[XrFytwobQPOTn2QOInx8jg][hothouse][3]: SearchParseException[[hothouse][3]:  
> from[-1],size[-1]: Parse Failure [Failed to parse source  
> [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
> nested: IllegalArgumentException[Clauses must have same field.];  
> }{[XrFytwobQPOTn2QOInx8jg][hothouse][1]:  
> SearchParseException[[hothouse][1]: from[-1],size[-1]: Parse Failure  
> [Failed to parse source  
> [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
> nested: IllegalArgumentException[Clauses must have same field.]; }]
> - status: 500
> 
> }
> 
> The documentation states the the clauses to a span\_or (or any of the span  
> queries ) must be span\_type queries. I assume there is more to it than  
> this based on the above error?
> 
> I also have a follow up question to the above... If I swap out the  
> span\_or w/ clauses for a boolean/should structure, the request above runs  
> (although I will have issues as I start having to support more complex  
> proximity queries taking this approach) but I don't get any highlighting  
> back for the request.. However, if I run the the request using match query  
> of type phrase with the slop of 6 I do get highlight results. The highlight  
> clause of the query structure is the same between the two approaches. Is  
> anyone aware of highlighting issues with span queries?
> 
> Thanks for any insights...
> 
> Curt

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Curt\_Kohler](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@Curt\_Kohler](https://discuss.elastic.co/u/Curt_Kohler)\
**Post date:** [September 9, 2013, 5:11pm UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525/3 "2013-09-09T17:11:58Z")

</div>

Thanks for the quick reply Luca,

As to your follow up question regarding highlighting, I'm using the fast  
vector highlighter in both cases..

Curt

On Monday, September 9, 2013 12:47:45 PM UTC-4, Luca Cavanna wrote:

> Hi,  
> the error that you get back is because all clauses must be related to the  
> same field, beyond being span\_term queries. In your case you are referring  
> to both field1 and field2. Span queries can be complex but always need to  
> stay on the same field.
> 
> There might be issues while highlighting when using span queries in  
> lucene, depending on the highlighter type you're using. Plain highlighter  
> of fast vector highlighter?
> 
> On Monday, September 9, 2013 6:34:15 PM UTC+2, Curt Kohler wrote:
> 
> > I'm in the process of trying to understand span queries and am having  
> > running into so problems that I can't find an answer for in the  
> > documentation. I was hoping someone on the group could shed some light.
> > 
> > I have loaded up some documents that have analyzed textual fields and I  
> > have a requirement to support proximity based queries in addition to normal  
> > textual queries. These documents are presented to a user in a search form  
> > with searchable fields that can map to multiple index fields. E.g search  
> > form fieldA maps to field1 and field2 in the index. An example  
> > proximity search might be fieldA(Pain PRE/6 Morphine). Since order is  
> > important, that leads me down the road to a SPAN\_NEAR query with "in\_order"  
> > set to true. Since term queries don't analyze the text, I need to do that  
> > prior to sending the search down (e.g. Morphine would need to be morphin  
> > with the analyzers we are using on those fields). Doing a single span\_near  
> > query on one of the two fields works, but when I try to combine them with a  
> > span\_or query as below I get a parse exception
> > 
> > {  
> > "query": {  
> > "span\_or": {  
> > "clauses": [  
> > {  
> > "span\_near": {  
> > "clauses": [  
> > {  
> > "span\_term": {  
> > "field1": "pain"  
> > }  
> > },  
> > {  
> > "span\_term": {  
> > "field1": "morphin"  
> > }  
> > }  
> > ],  
> > "slop": 6,  
> > "in\_order": true  
> > }  
> > },  
> > {  
> > "span\_near": {  
> > "clauses": [  
> > {  
> > "span\_term": {  
> > "field2": "pain"  
> > }  
> > },  
> > {  
> > "span\_term": {  
> > "field2": "morphin"  
> > }  
> > }  
> > ],  
> > "slop": 6,  
> > "in\_order": true  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > Gives  
> > {
> > 
> > - error: SearchPhaseExecutionException[Failed to execute phase  
> > [query], total failure; shardFailures  
> > {[XrFytwobQPOTn2QOInx8jg][hothouse][3]: SearchParseException[[hothouse][3]:  
> > from[-1],size[-1]: Parse Failure [Failed to parse source  
> > [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
> > nested: IllegalArgumentException[Clauses must have same field.];  
> > }{[XrFytwobQPOTn2QOInx8jg][hothouse][1]:  
> > SearchParseException[[hothouse][1]: from[-1],size[-1]: Parse Failure  
> > [Failed to parse source  
> > [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]];  
> > nested: IllegalArgumentException[Clauses must have same field.]; }]
> > - status: 500
> > 
> > }
> > 
> > The documentation states the the clauses to a span\_or (or any of the span  
> > queries ) must be span\_type queries. I assume there is more to it than  
> > this based on the above error?
> > 
> > I also have a follow up question to the above... If I swap out the  
> > span\_or w/ clauses for a boolean/should structure, the request above runs  
> > (although I will have issues as I start having to support more complex  
> > proximity queries taking this approach) but I don't get any highlighting  
> > back for the request.. However, if I run the the request using match query  
> > of type phrase with the slop of 6 I do get highlight results. The highlight  
> > clause of the query structure is the same between the two approaches. Is  
> > anyone aware of highlighting issues with span queries?
> > 
> > Thanks for any insights...
> > 
> > Curt

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 9, 2013, 5:55pm UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525/4 "2013-09-09T17:55:00Z")

</div>

Hi Curt,  
do you mid giving a try to the plain highlighter with the same queries and see what happens?

You can force the use of the plain one even though you stored term vectors by using "type":"plain" in your highlight json object.

On 9 Sep 2013, at 19:11, Curt Kohler [c.kohler@elsevier.com](mailto:c.kohler@elsevier.com) wrote:

> Thanks for the quick reply Luca,
> 
> As to your follow up question regarding highlighting, I'm using the fast vector highlighter in both cases..
> 
> Curt
> 
> On Monday, September 9, 2013 12:47:45 PM UTC-4, Luca Cavanna wrote:
> 
> > Hi,  
> > the error that you get back is because all clauses must be related to the same field, beyond being span\_term queries. In your case you are referring to both field1 and field2. Span queries can be complex but always need to stay on the same field.
> > 
> > There might be issues while highlighting when using span queries in lucene, depending on the highlighter type you're using. Plain highlighter of fast vector highlighter?
> > 
> > On Monday, September 9, 2013 6:34:15 PM UTC+2, Curt Kohler wrote:
> > 
> > > I'm in the process of trying to understand span queries and am having running into so problems that I can't find an answer for in the documentation. I was hoping someone on the group could shed some light.
> > > 
> > > I have loaded up some documents that have analyzed textual fields and I have a requirement to support proximity based queries in addition to normal textual queries. These documents are presented to a user in a search form with searchable fields that can map to multiple index fields. E.g search form fieldA maps to field1 and field2 in the index. An example proximity search might be fieldA(Pain PRE/6 Morphine). Since order is important, that leads me down the road to a SPAN\_NEAR query with "in\_order" set to true. Since term queries don't analyze the text, I need to do that prior to sending the search down (e.g. Morphine would need to be morphin with the analyzers we are using on those fields). Doing a single span\_near query on one of the two fields works, but when I try to combine them with a span\_or query as below I get a parse exception
> > > 
> > > {  
> > > "query": {  
> > > "span\_or": {  
> > > "clauses": [  
> > > {  
> > > "span\_near": {  
> > > "clauses": [  
> > > {  
> > > "span\_term": {  
> > > "field1": "pain"  
> > > }  
> > > },  
> > > {  
> > > "span\_term": {  
> > > "field1": "morphin"  
> > > }  
> > > }  
> > > ],  
> > > "slop": 6,  
> > > "in\_order": true  
> > > }  
> > > },  
> > > {  
> > > "span\_near": {  
> > > "clauses": [  
> > > {  
> > > "span\_term": {  
> > > "field2": "pain"  
> > > }  
> > > },  
> > > {  
> > > "span\_term": {  
> > > "field2": "morphin"  
> > > }  
> > > }  
> > > ],  
> > > "slop": 6,  
> > > "in\_order": true  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > Gives  
> > > {  
> > > error: SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[XrFytwobQPOTn2QOInx8jg][hothouse][3]: SearchParseException[[hothouse][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]]; nested: IllegalArgumentException[Clauses must have same field.]; }{[XrFytwobQPOTn2QOInx8jg][hothouse][1]: SearchParseException[[hothouse][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"span\_or":{"clauses":[{"span\_near":{"clauses":[{"span\_term":{"field1":"pain"}},{"span\_term":{"field1":"morphin"}}],"slop":6,"in\_order":true}},{"span\_near":{"clauses":[{"span\_term":{"field2":"pain"}},{"span\_term":{"field2":"morphin"}}],"slop":6,"in\_order":true}}]}}}]]]; nested: IllegalArgumentException[Clauses must have same field.]; }]  
> > > status: 500  
> > > }
> > > 
> > > The documentation states the the clauses to a span\_or (or any of the span queries ) must be span\_type queries. I assume there is more to it than this based on the above error?
> > > 
> > > I also have a follow up question to the above... If I swap out the span\_or w/ clauses for a boolean/should structure, the request above runs (although I will have issues as I start having to support more complex proximity queries taking this approach) but I don't get any highlighting back for the request.. However, if I run the the request using match query of type phrase with the slop of 6 I do get highlight results. The highlight clause of the query structure is the same between the two approaches. Is anyone aware of highlighting issues with span queries?
> > > 
> > > Thanks for any insights...
> > > 
> > > Curt
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/daTQwlBMDIE/unsubscribe](https://groups.google.com/d/topic/elasticsearch/daTQwlBMDIE/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:17am UTC](https://discuss.elastic.co/t/a-couple-of-span-query-questions/13525/5 "2017-07-06T02:17:25Z")

</div>


