Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing query parser in WrapperQueryParser produces invalid queries #1143

Closed
melix opened this issue Jul 21, 2011 · 2 comments

Comments

@melix
Copy link
Contributor

commented Jul 21, 2011

I was surprised to see that when I used the WrapperQuery, my query returned the source field instead of the fields I was asking for. Also, some queries combining wrapper queries where invalid (na) without any good explanation. I eventually found that writing this :

{
  "from" : 0,
  "size" : 10,
  "query" : {
    "wrapper" : {
      "query" : "ewogICAgInRlcm0iOiB7ImZpZWxkMSI6InZhbHVlMV8xIn0KfQo="
    }
  },
  "fields" : [ "field2" ]
}

Returns the "field2" field value :

{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"test","_type":"type1","_id":"1","_score":0.30685282,"fields":{"field2":"value2_1"}}]}}

, while writing this :

{
  "fields" : [ "field2" ],
  "from" : 0,
  "size" : 10,
  "query" : {
    "wrapper" : {
      "query" : "ewogICAgInRlcm0iOiB7ImZpZWxkMSI6InZhbHVlMV8xIn0KfQo="
    }
  }
}

doesn't and returns the source object :

{"took":4479,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"test","_type":"type1","_id":"1","_score":0.30685282, "_source" : {"field1":"value1_1","field2":"value2_1"}}]}}

. I guess there is something breaking the internal state of the parser when reusing it with SearchContext.current().queryParserService().parse(qSourceParser).query();

@melix

This comment has been minimized.

Copy link
Contributor Author

commented Jul 21, 2011

It seems that creating a temporary QueryParseContext fixes the problem :

   @Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        XContentParser.Token token = parser.nextToken();
        assert token == XContentParser.Token.FIELD_NAME;
        String fieldName = parser.currentName();
        assert fieldName.equals("query");
        parser.nextToken();

        byte[] querySource = parser.binaryValue();
        XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource);
        try {
            final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParser);
            context.reset(qSourceParser);
            Query result = context.parseInnerQuery();
            parser.nextToken();
            return result;
        } finally {
            qSourceParser.close();
        }
    }

It fixes both problems initially reported : broken queries and missing fields.

@kimchy

This comment has been minimized.

Copy link
Member

commented Jul 21, 2011

Yea, thats the problem!, good catch. Will push a fix to both master and 0.17 branch.

@kimchy kimchy closed this in ba137df Jul 21, 2011

mute pushed a commit to mute/elasticsearch that referenced this issue Jul 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.