# Starts With Search

**URL:** <https://discuss.elastic.co/t/starts-with-search/4588>\
**Category:** Elasticsearch\
**Created:** [June 9, 2011, 2:49pm UTC](https://discuss.elastic.co/t/starts-with-search/4588 "2011-06-09T14:49:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [June 9, 2011, 2:49pm UTC](https://discuss.elastic.co/t/starts-with-search/4588/1 "2011-06-09T14:49:38Z")

</div>

Hi,

Is there a way I can do a search that will automatically filter a  
resultset like this which is on an Analysed field:

Search String = mo\*:

```
Anti Money Laundering Analyst
Anti-Money Laundering CompOffi
Exec.Mgr.-Money Market Trading
Exec.Mgr.-Risk Modeling
Exp. Money Market Trader
Exp. Risk Modeler
Jun. Money Market Trader
Jun. Risk Modeler
Mgr.-Money Market Trading
Mgr.-Risk Modeling
Modeling Manager
Modeller
Money Market Dealer
Money Market Trader
Porter/Mover
Risk Modeler
Sen. Money Market Trader
Sen. Risk Modeler
Sen.Mgr.-Money Market Trading
Sen.Mgr.-Risk Modeling
Teaml.2-Risk Modeling

```

To This:

Search String = mo\*:

```
Modeling Manager
Modeller
Money Market Dealer
Money Market Trader

```

Best Regards,

David.

---

<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:** [June 9, 2011, 3:07pm UTC](https://discuss.elastic.co/t/starts-with-search/4588/2 "2011-06-09T15:07:50Z")

</div>

Hi David

> Is there a way I can do a search that will automatically filter a  
> resultset like this which is on an Analysed field:

> Search String = mo\*:
> 
> ```
> Modeling Manager
> Modeller
> Money Market Dealer
> Money Market Trader
> 
> ```

The only query type which allows you to anchor a term to the beginning  
of the field is span\_first.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Example usage:

> <https://gist.github.com/clintongormley/1016898>

Unfortunately, span-type queries only accept other span queries, so you  
can't use the wildcard or prefix queries. In order to achieve what you  
want, you'd need to find all the terms beginning with 'mo' in a separate  
facets query, and create a span\_term query for each term, all wrapped up  
in a span\_or query.

Not ideal I know...

the alternative would be to maintain another version of the field which  
is not analyzed (or uses eg just the lower case token filter)

clint

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [June 9, 2011, 4:14pm UTC](https://discuss.elastic.co/t/starts-with-search/4588/3 "2011-06-09T16:14:05Z")

</div>

Hi Clinton,

thanks for your suggestions, I was thinking of going with maintaining  
an extra non-analysed field, since I have once already for sorting,  
but trying this out in lucene, this type of field seems only to match  
on the whole string:

e.g.

seach = Money\* returns nothing

but

search = Money Market Dealer\* would return "Money Market Dealer"

I could be wrong because this was a quick test I did on an in-memory  
index I was creating in Lucene.

Best Regards,

David.

On Jun 9, 4:07 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi David
> 
> > Is there a way I can do a search that will automatically filter a  
> > resultset like this which is on an Analysed field:  
> > Search String = mo\*:
> 
> > ```
> > Modeling Manager
> > Modeller
> > Money Market Dealer
> > Money Market Trader
> > 
> > ```
> 
> The only query type which allows you to anchor a term to the beginning  
> of the field is span\_first.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/span-first-que)...
> 
> Example usage:[span\_first query example · GitHub](https://gist.github.com/1016898)
> 
> Unfortunately, span-type queries only accept other span queries, so you  
> can't use the wildcard or prefix queries. In order to achieve what you  
> want, you'd need to find all the terms beginning with 'mo' in a separate  
> facets query, and create a span\_term query for each term, all wrapped up  
> in a span\_or query.
> 
> Not ideal I know...
> 
> the alternative would be to maintain another version of the field which  
> is not analyzed (or uses eg just the lower case token filter)
> 
> clint

---

<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:** [June 9, 2011, 4:49pm UTC](https://discuss.elastic.co/t/starts-with-search/4588/4 "2011-06-09T16:49:57Z")

</div>

Hi David

> thanks for your suggestions, I was thinking of going with maintaining  
> an extra non-analysed field, since I have once already for sorting,  
> but trying this out in lucene, this type of field seems only to match  
> on the whole string:
> 
> e.g.  
> seach = Money\* returns nothing
> 
> but  
> search = Money Market Dealer\* would return "Money Market Dealer"

This depends on:

1. how your field is analyzed (or not)
2. how you are doing your search

If you post a gist reproduction then we will be more able to help you.  
(see [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help) )

clint

---

<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, 4:04am UTC](https://discuss.elastic.co/t/starts-with-search/4588/5 "2017-07-06T04:04:16Z")

</div>


