# Forcing Analysis of Terms and Span Terms?

**URL:** <https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838>\
**Category:** Elasticsearch\
**Created:** [November 27, 2012, 8:08am UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838 "2012-11-27T08:08:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [November 27, 2012, 8:08am UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/1 "2012-11-27T08:08:13Z")

</div>

Is there any way to force term queries to be analyzed? How about span\_term  
queries?

I would like to use the span\_near query but it only accepts other span-type  
queries as part of its clauses. The other span-type queries are not  
analyzed, so you cannot construct a span\_near query with analyzed  
sub-parts. For example, suppose you wanted a a query looking for "singing"  
within a two word span of "birds". Simply returning a span\_near with  
"singing" and "birds" as the clauses will yield zero results because the  
analyzer was not run and did not turn "singing" into "sing" and "birds"  
into "bird".

An alternative, I suppose, is to write my own analyzer and run it while  
constructing the query. Another option is to make separate calls to  
elasticsearch to run the analysis on the terms. Of course, I would prefer  
to not have to do either solution. Any suggestions?

Thanks,

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 27, 2012, 8:28am UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/2 "2012-11-27T08:28:59Z")

</div>

Hiya

> I would like to use the span\_near query but it only accepts other  
> span-type queries as part of its clauses. The other span-type queries  
> are not analyzed, so you cannot construct a span\_near query with  
> analyzed sub-parts. For example, suppose you wanted a a query looking  
> for "singing" within a two word span of "birds". Simply returning a  
> span\_near with "singing" and "birds" as the clauses will yield zero  
> results because the analyzer was not run and did not turn "singing"  
> into "sing" and "birds" into "bird".

For the example you give, which is fairly simple, you could just use a  
match\_phrase query:

{ "match\_phrase": {  
"my\_field": {  
"query": "singing birds",  
"slop": 3  
}  
}

Not quite as much control as when using span queries, but it'd serve the  
purpose you describe

clint

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 27, 2012, 9:20am UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/3 "2012-11-27T09:20:13Z")

</div>

I instantiate Elasticsearch analyzers on the client side and analyze the  
terms myself, without making a separate call to Elasticsearch. You can  
create analyzers using the AnalysisModule.

--  
Ivan

On Mon, Nov 26, 2012 at 10:08 PM, Michael Sander  
[michael.sander@gmail.com](mailto:michael.sander@gmail.com)wrote:

> Is there any way to force term queries to be analyzed? How about span\_term  
> queries?
> 
> I would like to use the span\_near query but it only accepts other  
> span-type queries as part of its clauses. The other span-type queries are  
> not analyzed, so you cannot construct a span\_near query with analyzed  
> sub-parts. For example, suppose you wanted a a query looking for "singing"  
> within a two word span of "birds". Simply returning a span\_near with  
> "singing" and "birds" as the clauses will yield zero results because the  
> analyzer was not run and did not turn "singing" into "sing" and "birds"  
> into "bird".
> 
> An alternative, I suppose, is to write my own analyzer and run it while  
> constructing the query. Another option is to make separate calls to  
> elasticsearch to run the analysis on the terms. Of course, I would prefer  
> to not have to do either solution. Any suggestions?
> 
> Thanks,
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [November 27, 2012, 2:17pm UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/4 "2012-11-27T14:17:38Z")

</div>

I am running python and using haystack to talk to the elasticsearch  
instance. Maybe I can copy the analysis code from Whoosh.

Michael Sander

On Tue, Nov 27, 2012 at 6:20 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> I instantiate Elasticsearch analyzers on the client side and analyze the  
> terms myself, without making a separate call to Elasticsearch. You can  
> create analyzers using the AnalysisModule.
> 
> --  
> Ivan
> 
> On Mon, Nov 26, 2012 at 10:08 PM, Michael Sander \<[michael.sander@gmail.com](mailto:michael.sander@gmail.com)
> 
> > wrote:
> 
> > Is there any way to force term queries to be analyzed? How about  
> > span\_term queries?
> > 
> > I would like to use the span\_near query but it only accepts other  
> > span-type queries as part of its clauses. The other span-type queries are  
> > not analyzed, so you cannot construct a span\_near query with analyzed  
> > sub-parts. For example, suppose you wanted a a query looking for "singing"  
> > within a two word span of "birds". Simply returning a span\_near with  
> > "singing" and "birds" as the clauses will yield zero results because the  
> > analyzer was not run and did not turn "singing" into "sing" and "birds"  
> > into "bird".
> > 
> > An alternative, I suppose, is to write my own analyzer and run it while  
> > constructing the query. Another option is to make separate calls to  
> > elasticsearch to run the analysis on the terms. Of course, I would prefer  
> > to not have to do either solution. Any suggestions?
> > 
> > Thanks,
> > 
> > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [November 27, 2012, 11:40pm UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/5 "2012-11-27T23:40:23Z")

</div>

@Clinton - Good point, but that was just a toy example. I want to be able  
to perform these complex queries.

@Ivan I am in a pure-python environment, so I can't just instantiate an  
AnalysisModule.

I think I have two options: (1) make a call to Elasticsearch (a url fetch)  
to analyze the tokens or (2) do the analysis in pure python.

Option 1 is probably easy to implement but has the obvious disadvantage of  
making a URL fetch, to analyze each token.  
Option 2 may be faster than making a URL fetch, but in pure python it could  
still be slow. Putting speed aside, a bigger problem may arise if the  
python analyzer does not match up exactly with the Elasticsearch analyzer.  
I am also loath to write my own analyzer in python.

I think, for now, I am going to go with option 2 and hope that the  
analyzers are the same. I found this pure-python stemming python (  
[stemming · PyPI](http://pypi.python.org/pypi/stemming/1.0)) which appears to do what I want.

I'll let you know how it goes.

Michael Sander

On Tue, Nov 27, 2012 at 11:17 PM, Michael Sander  
[michael.sander@gmail.com](mailto:michael.sander@gmail.com)wrote:

> I am running python and using haystack to talk to the elasticsearch  
> instance. Maybe I can copy the analysis code from Whoosh.
> 
> Michael Sander
> 
> On Tue, Nov 27, 2012 at 6:20 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > I instantiate Elasticsearch analyzers on the client side and analyze the  
> > terms myself, without making a separate call to Elasticsearch. You can  
> > create analyzers using the AnalysisModule.
> > 
> > --  
> > Ivan
> > 
> > On Mon, Nov 26, 2012 at 10:08 PM, Michael Sander \<  
> > [michael.sander@gmail.com](mailto:michael.sander@gmail.com)\> wrote:
> > 
> > > Is there any way to force term queries to be analyzed? How about  
> > > span\_term queries?
> > > 
> > > I would like to use the span\_near query but it only accepts other  
> > > span-type queries as part of its clauses. The other span-type queries are  
> > > not analyzed, so you cannot construct a span\_near query with analyzed  
> > > sub-parts. For example, suppose you wanted a a query looking for "singing"  
> > > within a two word span of "birds". Simply returning a span\_near with  
> > > "singing" and "birds" as the clauses will yield zero results because the  
> > > analyzer was not run and did not turn "singing" into "sing" and "birds"  
> > > into "bird".
> > > 
> > > An alternative, I suppose, is to write my own analyzer and run it while  
> > > constructing the query. Another option is to make separate calls to  
> > > elasticsearch to run the analysis on the terms. Of course, I would prefer  
> > > to not have to do either solution. Any suggestions?
> > > 
> > > Thanks,
> > > 
> > > --
> > 
> > --

--

---

<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:02am UTC](https://discuss.elastic.co/t/forcing-analysis-of-terms-and-span-terms/9838/6 "2017-07-06T03:02:33Z")

</div>


