# User friendly/google-like queries

**URL:** <https://discuss.elastic.co/t/user-friendly-google-like-queries/3824>\
**Category:** Elasticsearch\
**Created:** [January 24, 2011, 3:38pm UTC](https://discuss.elastic.co/t/user-friendly-google-like-queries/3824 "2011-01-24T15:38:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Warski](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Adam\_Warski](https://discuss.elastic.co/u/Adam_Warski)\
**Post date:** [January 24, 2011, 3:38pm UTC](https://discuss.elastic.co/t/user-friendly-google-like-queries/3824/1 "2011-01-24T15:38:55Z")

</div>

I am wondering if ElasticSearch provides any utilities or if there are  
some guidelines for implementing user-friendly queries?

I suppose the query would need to be somehow translated into a form  
understandable by Lucene/ElasticSearch.  
This means probably:

- escaping special characters
- treating +word, -word appropriately
- treating "word word" as a phrase
- being tolerant for unclosed " and '

--  
Regards,  
Adam Warski

---

<div class="post-metadata">

**Author:** ![Joaquin\_Cuenca\_Abela](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Joaquin\_Cuenca\_Abela](https://discuss.elastic.co/u/Joaquin_Cuenca_Abela)\
**Post date:** [January 24, 2011, 9:18pm UTC](https://discuss.elastic.co/t/user-friendly-google-like-queries/3824/2 "2011-01-24T21:18:42Z")

</div>

Hi Adam,

maybe there is a better way to do it, but what I'm doing is sending  
the user's query to elasticsearch, and if I get back a 500, I  
"sanitize" the query doing (in Python):

q = re.sub(r'([-+!(){}^"~\*?:\]|&&|||)', r'\\1', q.strip())  
q = re.sub(r'(^|(?\<=\W))AND($|(?=\W))', 'and', q)  
q = re.sub(r'(^|(?\<=\W))OR($|(?=\W))', 'or', q)  
q = re.sub(r'(^|(?\<=\W))NOT($|(?=\W))', 'not', q)

The first line removes "dangerous" characters, and the next 3 lines  
remove instances of AND, OR and NOT at the beginning or at the end of  
the query.

I'm also interested on more graceful ways to deal with failures, but  
so far this has been good enough for me.

Cheers,

On Mon, Jan 24, 2011 at 4:38 PM, Adam Warski [adamwtw@gmail.com](mailto:adamwtw@gmail.com) wrote:

> I am wondering if Elasticsearch provides any utilities or if there are  
> some guidelines for implementing user-friendly queries?
> 
> I suppose the query would need to be somehow translated into a form  
> understandable by Lucene/Elasticsearch.  
> This means probably:
> 
> - escaping special characters
> - treating +word, -word appropriately
> - treating "word word" as a phrase
> - being tolerant for unclosed " and '
> 
> --  
> Regards,  
> Adam Warski

--  
Joaquin Cuenca Abela

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [February 5, 2011, 6:21pm UTC](https://discuss.elastic.co/t/user-friendly-google-like-queries/3824/3 "2011-02-05T18:21:03Z")

</div>

I would be also interested 🙂

On 24 Jan., 22:18, Joaquin Cuenca Abela [joaq...@cuencaabela.com](mailto:joaq...@cuencaabela.com)  
wrote:

> Hi Adam,
> 
> maybe there is a better way to do it, but what I'm doing is sending  
> the user's query to elasticsearch, and if I get back a 500, I  
> "sanitize" the query doing (in Python):
> 
> q = re.sub(r'([-+!(){}^"~\*?:\]|&&|||)', r'\\1', q.strip())  
> q = re.sub(r'(^|(?\<=\W))AND($|(?=\W))', 'and', q)  
> q = re.sub(r'(^|(?\<=\W))OR($|(?=\W))', 'or', q)  
> q = re.sub(r'(^|(?\<=\W))NOT($|(?=\W))', 'not', q)
> 
> The first line removes "dangerous" characters, and the next 3 lines  
> remove instances of AND, OR and NOT at the beginning or at the end of  
> the query.
> 
> I'm also interested on more graceful ways to deal with failures, but  
> so far this has been good enough for me.
> 
> Cheers,
> 
> On Mon, Jan 24, 2011 at 4:38 PM, Adam Warski [adam...@gmail.com](mailto:adam...@gmail.com) wrote:
> 
> > I am wondering if Elasticsearch provides any utilities or if there are  
> > some guidelines for implementing user-friendly queries?
> 
> > I suppose the query would need to be somehow translated into a form  
> > understandable by Lucene/Elasticsearch.  
> > This means probably:
> > 
> > - escaping special characters
> > - treating +word, -word appropriately
> > - treating "word word" as a phrase
> > - being tolerant for unclosed " and '
> 
> > --  
> > Regards,  
> > Adam Warski
> 
> --  
> Joaquin Cuenca Abela

---

<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:12am UTC](https://discuss.elastic.co/t/user-friendly-google-like-queries/3824/4 "2017-07-06T04:12:35Z")

</div>


