# Cant create XContentParser from source field

**URL:** <https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887>\
**Category:** Elasticsearch\
**Created:** [June 3, 2015, 11:29pm UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887 "2015-06-03T23:29:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaoManhDat](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@CaoManhDat](https://discuss.elastic.co/u/CaoManhDat)\
**Post date:** [June 3, 2015, 11:29pm UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/1 "2015-06-03T23:29:36Z")

</div>

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

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

```

exception

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

```

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 4, 2015, 1:45am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/2 "2015-06-04T01:45:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CaoManhDat](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@CaoManhDat](https://discuss.elastic.co/u/CaoManhDat)\
**Post date:** [June 4, 2015, 2:13am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/3 "2015-06-04T02:13:09Z")

</div>

I have some a test like this

```java
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

```java
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.

---

<div class="post-metadata">

**Author:** ![CaoManhDat](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@CaoManhDat](https://discuss.elastic.co/u/CaoManhDat)\
**Post date:** [June 5, 2015, 12:48am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/4 "2015-06-05T00:48:19Z")

</div>

Anyon have any clue?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 5, 2015, 1:24am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/5 "2015-06-05T01:24:54Z")

</div>

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:

> **[imotov/elasticsearch](https://github.com/imotov/elasticsearch/tree/add-source-access-in-script-test)**
>
> elasticsearch - Open Source, Distributed, RESTful Search Engine

Everything seems to work fine.

---

<div class="post-metadata">

**Author:** ![CaoManhDat](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@CaoManhDat](https://discuss.elastic.co/u/CaoManhDat)\
**Post date:** [June 8, 2015, 4:42am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/6 "2015-06-08T04:42:00Z")

</div>

I extends CustomScoreQuery of Lucene and wrapper it on SearchService

```java
//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!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 26, 2015, 5:18pm UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/7 "2015-06-26T17:18:27Z")

</div>

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.

---

<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, 12:05am UTC](https://discuss.elastic.co/t/cant-create-xcontentparser-from-source-field/1887/8 "2017-07-06T00:05:09Z")

</div>


