Cant create XContentParser from source field

I'm currently create a CustomQuery, i need to parse _source field to get some specific field. But it some time throw ElasticsearchParseException

BytesArray bytes = new BytesArray(doc.getField("_source").binaryValue());
XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes);

exception

org.elasticsearch.ElasticsearchParseException: Failed to derive xcontent from org.elasticsearch.common.bytes.BytesArray@d94ffdcd
    at org.elasticsearch.common.xcontent.XContentFactory.xContent(XContentFactory.java:259)

Can you show how your bytes look like? What have you got there?

I have some a test like this

createIndex("test1");
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("url", "http://google.com", "content", "this site be long to google", "tempField", "fdafdasfdafasdf"),
        client().prepareIndex("test", "type1", "2").setSource("url", "http://facebook.com", "content", "this site be long to facebook", "tempField", "fdafdasfdafasdf"),
        client().prepareIndex("test", "type1", "3").setSource("url", "http://vnexpress.net", "content", "this site be long to vnexpress", "tempField", "fdafdasfdafasdf"),
        client().prepareIndex("test", "type1", "4").setSource("url", "http://pintulac.com.ec.com", "content", "this site be long to bad site", "tempField", "fdafdasfdafasdf"));
SearchResponse response = client().prepareSearch().setQuery(queryStringQuery("google")).setExplain(true).get();
System.out.println(response.getHits().getHits()[0].getSource());
System.out.println(response.getHits().getHits()[0].getScore());
System.out.println(response.getHits().getHits()[0].explanation().toString());

and in my custom score query

AtomicReader r = context.reader();
Document doc = r.document(docId);
String url = doc.get(urlField);
if(url == null){
  try{
    BytesArray bytes = new BytesArray(doc.getField("_source").binaryValue());
    XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes); //exception
......

Thanks in advance.

Anyon have any clue?

I tried to reproduce the issue, but there are too many details missing in your description. Could you just dump the bytes that you are trying to parse and paste them here?

By the way, this is the repro that I have based on the information that you have provided so far:

Everything seems to work fine.

I extends CustomScoreQuery of Lucene and wrapper it on SearchService

//inside SearchService createContext

if(context.size() == -1) {
  context.size(10);
}

ParserQuery = parserQuery = context.parsedQuery();
if(parsedQuery != null && parserQuery.query() != null){
  ParserQuery newParsedQuery = new ParsedQuery(new CustomQuery(parsedQuery.query()), parsedQuery);
  context.parsedQuery(newParsedQuery);
}

But your source test is quite interesting! Thanks!

Sorry, I am not really sure what you are trying to do but modifying SearchService doesn't sound like a good long-term approach to me. The query infrastructure is very pluggable, so you should be able to add a custom query as a plugin without modifying any eleasticsearch code.