# Search anchored at beginning/end of string with slop

**URL:** <https://discuss.elastic.co/t/search-anchored-at-beginning-end-of-string-with-slop/13305>\
**Category:** Elasticsearch\
**Created:** [August 22, 2013, 7:20pm UTC](https://discuss.elastic.co/t/search-anchored-at-beginning-end-of-string-with-slop/13305 "2013-08-22T19:20:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Dlug](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Paul\_Dlug](https://discuss.elastic.co/u/Paul_Dlug)\
**Post date:** [August 22, 2013, 7:20pm UTC](https://discuss.elastic.co/t/search-anchored-at-beginning-end-of-string-with-slop/13305/1 "2013-08-22T19:20:58Z")

</div>

I have a search case where it's desirable to anchor the query at the  
beginning and end of the string but allow slop in the middle. For example,  
I have field "friends" with a list of names in it as strings:

"A User"  
"B User"  
"A B User"  
"C User D"  
"C User"

The user needs to be able to enter a query like "A User" and have it match  
"A User" and "A B User" but not others (the reason for this is that they  
typically get the first initial correct and last name but may not know any  
middle initials). A search for "C User" should not match "C User D". This  
is in an application I'm porting from solr to Elasticsearch, in solr I had  
written a span query plugin to construct these and then surrounded each  
name with tokens: "^^^ A User $$$" so I could anchor span terms on the  
beginning and end of the strings.

I'm wondering what the translation of this is to elasticsearch, any help  
greatly appreciated.

Thanks,  
Paul

--  
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:** [August 23, 2013, 10:44am UTC](https://discuss.elastic.co/t/search-anchored-at-beginning-end-of-string-with-slop/13305/2 "2013-08-23T10:44:43Z")

</div>

Hi Paul,  
have a look at the elasticsearch query DSL[http://www.elasticsearch.org/guide/reference/query-dsl/](http://www.elasticsearch.org/guide/reference/query-dsl/),  
which exposes the lucene span queries as well. You combine them pretty much  
as you would do with lucene, but you have to express them in json format.  
In your case I'd have a look mainly at the span first query[http://www.elasticsearch.org/guide/reference/query-dsl/span-first-query/](http://www.elasticsearch.org/guide/reference/query-dsl/span-first-query/)  
, span near query[http://www.elasticsearch.org/guide/reference/query-dsl/span-near-query/](http://www.elasticsearch.org/guide/reference/query-dsl/span-near-query/).  
Also note that with span queries you have to use a span term query[http://www.elasticsearch.org/guide/reference/query-dsl/span-term-query/](http://www.elasticsearch.org/guide/reference/query-dsl/span-term-query/) to  
provide the term, which is essentially a basic term query that takes into  
account positions too.

Let me know how it went and don't hesitate to ask if you need any further  
information

Cheers  
Luca

On Thursday, August 22, 2013 9:20:58 PM UTC+2, Paul Dlug wrote:

> I have a search case where it's desirable to anchor the query at the  
> beginning and end of the string but allow slop in the middle. For example,  
> I have field "friends" with a list of names in it as strings:
> 
> "A User"  
> "B User"  
> "A B User"  
> "C User D"  
> "C User"
> 
> The user needs to be able to enter a query like "A User" and have it match  
> "A User" and "A B User" but not others (the reason for this is that they  
> typically get the first initial correct and last name but may not know any  
> middle initials). A search for "C User" should not match "C User D". This  
> is in an application I'm porting from solr to Elasticsearch, in solr I had  
> written a span query plugin to construct these and then surrounded each  
> name with tokens: "^^^ A User $$$" so I could anchor span terms on the  
> beginning and end of the strings.
> 
> I'm wondering what the translation of this is to elasticsearch, any help  
> greatly appreciated.
> 
> Thanks,  
> Paul

--  
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:19am UTC](https://discuss.elastic.co/t/search-anchored-at-beginning-end-of-string-with-slop/13305/3 "2017-07-06T02:19:59Z")

</div>


