# How to validate field value to search?

**URL:** <https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375>\
**Category:** Elasticsearch\
**Created:** [August 28, 2013, 5:59pm UTC](https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375 "2013-08-28T17:59:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discuss.elastic.co/u/Nova)\
**Post date:** [August 28, 2013, 5:59pm UTC](https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375/1 "2013-08-28T17:59:33Z")

</div>

## Hello everybody. Example i have a search query: {"query":{"bool":{"must":[],"must\_not":[],"should":[{"query\_string":{"default\_field":"tweet.user","query":" _kompa)_"}}]}},"from":0,"size":50,"sort":[],"facets":{}}, when i run this query on elasticsearch server i will got ParseException:

Caused by: org.apache.lucene.queryparser.classic.ParseException: Cannot  
parse '(  
kompa': Encountered "" at line 1, column 6.  
Was expecting one of:  
 ...  
 ...  
 ...  
"+" ...  
"-" ...  
 ...  
"(" ...  
")" ...  
"\*" ...  
"^" ...  
 ...  
 ...  
\<FUZZY\_SLOP\> ...  
 ...  
 ...  
 ...  
"[" ...  
"{" ...  
 ...

```
    at 

```

org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryPars  
erBase.java:126)

========================

But if this query:  
{"query":{"bool":{"must":[],"must\_not":[],"should":[{"query\_string":{"default\_field":"tweet.user","query":"  
_(kompa)_"}}]}},"from":0,"size":50,"sort":[],"facets":{}},  
this can do search well.

So i want to check field value before executing a search.

- _I don't know what rule that elasticsearch used to check the field value.  
I try on the [elasticsearc.org](http://elasticsearc.org) but couldn't found any thing.  
Because there are many case with many special characters to validate,  
example: {}$%^&_()@!`

Does anyone have some idea ? Thanks;

--  
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:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [August 28, 2013, 8:34pm UTC](https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375/2 "2013-08-28T20:34:51Z")

</div>

IMO this is just how the lucene query parser works, you need to escape  
those things.  
do you need to have support for query syntax at all like boolean operators?  
if not maybe match query  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/match-query/)) can  
help you

simon

On Wednesday, August 28, 2013 7:59:33 PM UTC+2, Duy Binh wrote:

> ## Hello everybody. Example i have a search query: {"query":{"bool":{"must":,"must\_not":,"should":[{"query\_string":{"default\_field":"tweet.user","query":" _kompa)_"}}]}},"from":0,"size":50,"sort":,"facets":{}}, when i run this query on elasticsearch server i will got ParseException:
> 
> Caused by: org.apache.lucene.queryparser.classic.ParseException: Cannot  
> parse '(  
> kompa': Encountered "" at line 1, column 6.  
> Was expecting one of:  
> ...  
> ...  
> ...  
> "+" ...  
> "-" ...  
> ...  
> "(" ...  
> ")" ...  
> "\*" ...  
> "^" ...  
> ...  
> ...  
> \<FUZZY\_SLOP\> ...  
> ...  
> ...  
> ...  
> "[" ...  
> "{" ...  
> ...
> 
> ```
> at 
> 
> ```
> 
> org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryPars  
> erBase.java:126)
> 
> ========================
> 
> But if this query:  
> {"query":{"bool":{"must":,"must\_not":,"should":[{"query\_string":{"default\_field":"tweet.user","query":"  
> _(kompa)_"}}]}},"from":0,"size":50,"sort":,"facets":{}},  
> this can do search well.
> 
> So i want to check field value before executing a search.
> 
> - _I don't know what rule that elasticsearch used to check the field  
> value. I try on the [elasticsearc.org](http://elasticsearc.org) but couldn't found any thing.  
> Because there are many case with many special characters to validate,  
> example: {}$%^&_()@!`
> 
> Does anyone have some idea ? Thanks;

--  
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:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discuss.elastic.co/u/Nova)\
**Post date:** [August 29, 2013, 7:21am UTC](https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375/3 "2013-08-29T07:21:27Z")

</div>

Thanks simmonw;  
I got that elasticsearch uses Java Lucene to parse and validation the query.

But with the query:  
{"query":{"bool":{"must":,"must\_not":,"should":[{"query\_string":{"default\_field":"tweet.user","query":"  
_(kompa)_"}}]}},"from":0,"size":50,"sort":,"facets":{}}  
it works; so i think no need to escape it. Do you think so ?

On Thu, Aug 29, 2013 at 3:34 AM, simonw  
[simon.willnauer@elasticsearch.com](mailto:simon.willnauer@elasticsearch.com)wrote:

> IMO this is just how the lucene query parser works, you need to escape  
> those things.  
> do you need to have support for query syntax at all like boolean  
> operators? if not maybe match query (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/match-query/)) can  
> help you
> 
> simon
> 
> On Wednesday, August 28, 2013 7:59:33 PM UTC+2, Duy Binh wrote:
> 
> > Hello everybody.  
> > Example i have a search query: {"query":{"bool":{"must":,"\*\*  
> > must\_not":,"should":[{" **query\_string":{"default\_field"**  
> > :"tweet.user","query":"_kompa)_"\*\*}}]}},"from":0,"size":50,"**sort":[],"facets":{}},  
> > when i run this query on elasticsearch server i will got ParseException:  
> > ------------------------------**-------------------------  
> > Caused by: org.apache.lucene.queryparser.\*\*classic.ParseException:  
> > Cannot parse '(  
> > kompa': Encountered "" at line 1, column 6.  
> > Was expecting one of:  
> > ...  
> > ...  
> > ...  
> > "+" ...  
> > "-" ...  
> > ...  
> > "(" ...  
> > ")" ...  
> > "\*" ...  
> > "^" ...  
> > ...  
> > ...  
> > \<FUZZY\_SLOP\> ...  
> > ...  
> > ...  
> > ...  
> > "[" ...  
> > "{" ...  
> > ...
> > 
> > ```
> > at org.apache.lucene.queryparser.**classic.QueryParserBase.parse(
> > 
> > ```
> > 
> > \*\*QueryPars  
> > erBase.java:126)
> > 
> > ========================
> > 
> > But if this query: {"query":{"bool":{"must":,"\*\*  
> > must\_not":,"should":[{" **query\_string":{"default\_field"**  
> > :"tweet.user","query":"_(kompa)_**"}}]}},"from":0,"size":50,"**  
> > sort":,"facets":{}},  
> > this can do search well.
> > 
> > So i want to check field value before executing a search.
> > 
> > - _I don't know what rule that elasticsearch used to check the field  
> > value. I try on the [elasticsearc.org](http://elasticsearc.org) but couldn't found any thing.  
> > Because there are many case with many special characters to validate,  
> > example: {}$%^&_()@!`
> > 
> > Does anyone have some idea ? Thanks;
> 
> --  
> 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/Sys8ClJ\_-EU/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Sys8ClJ_-EU/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:19am UTC](https://discuss.elastic.co/t/how-to-validate-field-value-to-search/13375/4 "2017-07-06T02:19:06Z")

</div>


