0

Is that possible that try to query an empty string in elasticsearch without error? The situation now in my case is there are some words is stored in index, I will receive some data, I want use this kind of data as query data. I can not control the data, so sometimes the data could be empty string. E.g.

query={"query":{"query_stirng":{"query":""}}}

Caused by: org.apache.lucene.queryparser.classic.ParseException: Cannot parse '   ': Encountered "<EOF>" at line 1, column 3.
Was expecting one of:
    <NOT> ...
    "+" ...
    "-" ...
    <BAREOPER> ...
    "(" ...
    "*" ...
    <QUOTED> ...
    <TERM> ...
    <PREFIXTERM> ...
    <WILDTERM> ...
    <REGEXPTERM> ...
    "[" ...
    "{" ...
    <NUMBER> ...
    <TERM> ...
    "*" ...

    at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:126)
    at org.elasticsearch.index.query.QueryStringQueryParser.parse(QueryStringQueryParser.java:213)
    ... 15 more
Caused by: org.apache.lucene.queryparser.classic.ParseException: Encountered "<EOF>" at line 1, column 3.
Was expecting one of:
    <NOT> ...
    "+" ...
    "-" ...
    <BAREOPER> ...
    "(" ...
    "*" ...
    <QUOTED> ...
    <TERM> ...
    <PREFIXTERM> ...
    <WILDTERM> ...
    <REGEXPTERM> ...
    "[" ...
    "{" ...
    <NUMBER> ...
    <TERM> ...
    "*" ...

    at org.apache.lucene.queryparser.classic.QueryParser.generateParseException(QueryParser.java:708)
    at org.apache.lucene.queryparser.classic.QueryParser.jj_consume_token(QueryParser.java:590)
    at org.apache.lucene.queryparser.classic.QueryParser.Clause(QueryParser.java:275)
    at org.apache.lucene.queryparser.classic.QueryParser.Query(QueryParser.java:181)
    at org.apache.lucene.queryparser.classic.QueryParser.TopLevelQuery(QueryParser.java:170)
    at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:121)
    ... 16 more

Is that possible to use elasticsearch to solve this problem or must use code the solve this? So that I can receive the empty data and query with elasticsearch and elasitcsearch will not cause any problem?

Environment : Ubuntu 12.04 desktop 64bit

elasticsearch 0.90.7 >> single node

programming language: python

3 Answers 3

1

I think maybe the missing filter will work in this case:

Missing Filter

{
  "query": {
    "filtered": {
      "filter": {
        "missing": {
          "field": "user",
          "existence": true,
          "null_value": true
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

OP ain't asking about missing fields. OP is asking about empty query search.
0

I guess its not possible as of now. You need to handle it in the code with some try-catch mechanism (Exception handling).

Comments

0

I encountered a similar problem with String fields containing empty string values. Using Query DSL, I managed to get empty strings by using: (if document contains "field" : "" )

 "query": {
       "query_string": {
          "query": "( _exists_:field && -field:* )"                 
       }
   }

this basically means, "field is not null, and it is not any string".

similarly, excluding empty strings would be:

   "query": {
       "query_string": {
          "query": "( _missing_:field || field:* )"                 
       }
   }

which means "field is null OR any string".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.