# How to index parts of json files using Java API?

**URL:** <https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604>\
**Category:** Elasticsearch\
**Created:** [July 1, 2013, 3:56pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604 "2013-07-01T15:56:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11122](https://avatars.discourse-cdn.com/v4/letter/1/6a8cbe/32.png) [@11122](https://discuss.elastic.co/u/11122)\
**Post date:** [July 1, 2013, 3:56pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/1 "2013-07-01T15:56:17Z")

</div>

I've parsed a json file into an JSONObject and created an index for this  
whole JSONObject.  
But how can I index only a part of this JSONObject, for instance, only  
several fields or nested parts?  
Should I extract the required parts of json object and create an another  
json object for indexing? I think there should be a better way to do this.  
Could anyone give my some advices? Thanks a lot!

--  
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:** ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)\
**Post date:** [July 2, 2013, 12:31am UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/2 "2013-07-02T00:31:42Z")

</div>

Hi,  
There are a couple of options...

The elasticsearch defaults are great for playing around, but it is almost  
always a good idea to disable dynamic mapping and setup explicit mappings(  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to configure what  
fields get indexed and which ones don't.

The next question is if you want to keep the original JSON or have  
elasticsearch strip out elements you don't want. You can accomplish this  
with source field include/excludes:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Best Regards,  
Paul

On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:

> I've parsed a json file into an JSONObject and created an index for this  
> whole JSONObject.  
> But how can I index only a part of this JSONObject, for instance, only  
> several fields or nested parts?  
> Should I extract the required parts of json object and create an another  
> json object for indexing? I think there should be a better way to do this.  
> Could anyone give my some advices? Thanks a lot!

--  
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:** ![11122](https://avatars.discourse-cdn.com/v4/letter/1/6a8cbe/32.png) [@11122](https://discuss.elastic.co/u/11122)\
**Post date:** [July 2, 2013, 10:49am UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/3 "2013-07-02T10:49:25Z")

</div>

Thanks a lot.  
I've read something about mapping and notice there is a "enabled" flag(  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type/)). I tried  
to create an explicit mapping with this flag, here is my code:  
String mapping =  
XContentFactory.jsonBuilder().startObject().startObject("testpbl")  
.startObject("properties")  
.startObject("person").field("type", "object")  
.startObject("properties")  
.startObject("name").field("type", "object")  
.field("enabled", false)  
.endObject()  
.startObject("sid").field("type","string").endObject()  
.endObject()  
.startObject("message").field("type", "string").endObject()  
.endObject().endObject().endObject().endObject()  
.string();

```
    node.client().admin()
            .indices().prepareCreate("pbl")
            .addMapping("testpbl", mapping)
            .execute().actionGet();

    JSONParser parser = new JSONParser();
    Object object = parser.parse(new FileReader("mydoc\\test.js"));
    JSONObject jsonData = (JSONObject) object;

    node.client().prepareIndex("pbl", "testpbl",

```

"1").setSource(jsonData).execute().actionGet();

and the test.js is:

{  
"tweet": {  
"person": {  
"name": {  
"first\_name": "Shay",  
"last\_name": "Banon"  
},  
"sid": "12345"  
},  
"message": "This is a tweet!"  
}  
}

the question is, it seems that my mapping doesn't work. When I tried to get  
my created index and check its content, I still get the name's content  
which should not be indexed according to my mapping.

is there any error with my code? I will appreciate for helping.

2013/7/2 ppearcy [ppearcy@gmail.com](mailto:ppearcy@gmail.com)

> Hi,  
> There are a couple of options...
> 
> The elasticsearch defaults are great for playing around, but it is almost  
> always a good idea to disable dynamic mapping and setup explicit mappings(  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to configure what  
> fields get indexed and which ones don't.
> 
> The next question is if you want to keep the original JSON or have  
> elasticsearch strip out elements you don't want. You can accomplish this  
> with source field include/excludes:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/source-field/)
> 
> Best Regards,  
> Paul
> 
> On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:
> 
> > I've parsed a json file into an JSONObject and created an index for this  
> > whole JSONObject.  
> > But how can I index only a part of this JSONObject, for instance, only  
> > several fields or nested parts?  
> > Should I extract the required parts of json object and create an another  
> > json object for indexing? I think there should be a better way to do this.  
> > Could anyone give my some advices? Thanks a lot!
> 
> --  
> 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 2, 2013, 1:59pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/4 "2013-07-02T13:59:49Z")

</div>

Just a note to this: There is a big difference between indexing and storing source document.

Elasticsearch won't touch your source document and will store it as is.  
For example, if you use carriage return after each field, you will get it back when doing get or search operations.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 juil. 2013 à 12:49, Chang Zhang [chang.zhang29@googlemail.com](mailto:chang.zhang29@googlemail.com) a écrit :

> Thanks a lot.  
> I've read something about mapping and notice there is a "enabled" flag([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type/)). I tried to create an explicit mapping with this flag, here is my code:  
> String mapping = XContentFactory.jsonBuilder().startObject().startObject("testpbl")  
> .startObject("properties")  
> .startObject("person").field("type", "object")  
> .startObject("properties")  
> .startObject("name").field("type", "object")  
> .field("enabled", false)  
> .endObject()  
> .startObject("sid").field("type","string").endObject()  
> .endObject()  
> .startObject("message").field("type", "string").endObject()  
> .endObject().endObject().endObject().endObject()  
> .string();
> 
> ```
> node.client().admin()
> .indices().prepareCreate("pbl")
> .addMapping("testpbl", mapping)
> .execute().actionGet();
> 
> JSONParser parser = new JSONParser();
> Object object = parser.parse(new FileReader("mydoc\\test.js"));
> JSONObject jsonData = (JSONObject) object;
> 
> node.client().prepareIndex("pbl", "testpbl", "1").setSource(jsonData).execute().actionGet();
> 
> ```
> 
> and the test.js is:
> 
> {  
> "tweet": {  
> "person": {  
> "name": {  
> "first\_name": "Shay",  
> "last\_name": "Banon"  
> },  
> "sid": "12345"  
> },  
> "message": "This is a tweet!"  
> }  
> }
> 
> the question is, it seems that my mapping doesn't work. When I tried to get my created index and check its content, I still get the name's content which should not be indexed according to my mapping.
> 
> is there any error with my code? I will appreciate for helping.
> 
> 2013/7/2 ppearcy [ppearcy@gmail.com](mailto:ppearcy@gmail.com)
> 
> > Hi,  
> > There are a couple of options...
> > 
> > The elasticsearch defaults are great for playing around, but it is almost always a good idea to disable dynamic mapping and setup explicit mappings([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to configure what fields get indexed and which ones don't.
> > 
> > The next question is if you want to keep the original JSON or have elasticsearch strip out elements you don't want. You can accomplish this with source field include/excludes:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/source-field/)
> > 
> > Best Regards,  
> > Paul
> > 
> > On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:
> > 
> > > I've parsed a json file into an JSONObject and created an index for this whole JSONObject.  
> > > But how can I index only a part of this JSONObject, for instance, only several fields or nested parts?  
> > > Should I extract the required parts of json object and create an another json object for indexing? I think there should be a better way to do this.  
> > > Could anyone give my some advices? Thanks a lot!
> > 
> > --  
> > 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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).

--  
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:** ![11122](https://avatars.discourse-cdn.com/v4/letter/1/6a8cbe/32.png) [@11122](https://discuss.elastic.co/u/11122)\
**Post date:** [July 2, 2013, 2:29pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/5 "2013-07-02T14:29:13Z")

</div>

Thank you.  
now I noticed that my mapping has been working to the index, because when I  
search the field which has been enabled false in the mapping cannot be  
hitted. So this means that this part is not indexed？ do I understand  
correctly?

but here is an another question.  
I add a field to be loaded and return in the searchresponse with  
addField("myfield"), it seems that there is no required field returned,  
here is my code and a screenshot of debug result.

SearchResponse searchResponse = node.client().prepareSearch("pbl")  
.setTypes("testpbl")  
.setQuery(QueryBuilders.matchQuery("sid","12345"))  
.addField("sid")  
.setExplain(true)  
.setSize(10)  
.execute().actionGet();  
[image: 内嵌图片 1]

2013/7/2 David Pilato [david@pilato.fr](mailto:david@pilato.fr)

> Just a note to this: There is a big difference between indexing and  
> storing source document.
> 
> Elasticsearch won't touch your source document and will store it as is.  
> For example, if you use carriage return after each field, you will get it  
> back when doing get or search operations.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 2 juil. 2013 à 12:49, Chang Zhang [chang.zhang29@googlemail.com](mailto:chang.zhang29@googlemail.com) a  
> écrit :
> 
> Thanks a lot.  
> I've read something about mapping and notice there is a "enabled" flag(  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type/)). I  
> tried to create an explicit mapping with this flag, here is my code:  
> String mapping =  
> XContentFactory.jsonBuilder().startObject().startObject("testpbl")  
> .startObject("properties")  
> .startObject("person").field("type", "object")  
> .startObject("properties")  
> .startObject("name").field("type", "object")  
> .field("enabled", false)  
> .endObject()  
> .startObject("sid").field("type","string").endObject()  
> .endObject()  
> .startObject("message").field("type", "string").endObject()  
> .endObject().endObject().endObject().endObject()  
> .string();
> 
> ```
> node.client().admin()
> .indices().prepareCreate("pbl")
> .addMapping("testpbl", mapping)
> .execute().actionGet();
> 
> JSONParser parser = new JSONParser();
> Object object = parser.parse(new FileReader("mydoc\\test.js"));
> JSONObject jsonData = (JSONObject) object;
> 
> node.client().prepareIndex("pbl", "testpbl",
> 
> ```
> 
> "1").setSource(jsonData).execute().actionGet();
> 
> and the test.js is:
> 
> {  
> "tweet": {  
> "person": {  
> "name": {  
> "first\_name": "Shay",  
> "last\_name": "Banon"  
> },  
> "sid": "12345"  
> },  
> "message": "This is a tweet!"  
> }  
> }
> 
> the question is, it seems that my mapping doesn't work. When I tried to  
> get my created index and check its content, I still get the name's content  
> which should not be indexed according to my mapping.
> 
> is there any error with my code? I will appreciate for helping.
> 
> 2013/7/2 ppearcy [ppearcy@gmail.com](mailto:ppearcy@gmail.com)
> 
> > Hi,  
> > There are a couple of options...
> > 
> > The elasticsearch defaults are great for playing around, but it is almost  
> > always a good idea to disable dynamic mapping and setup explicit mappings(  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to configure what  
> > fields get indexed and which ones don't.
> > 
> > The next question is if you want to keep the original JSON or have  
> > elasticsearch strip out elements you don't want. You can accomplish this  
> > with source field include/excludes:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/source-field/)
> > 
> > Best Regards,  
> > Paul
> > 
> > On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:
> > 
> > > I've parsed a json file into an JSONObject and created an index for this  
> > > whole JSONObject.  
> > > But how can I index only a part of this JSONObject, for instance, only  
> > > several fields or nested parts?  
> > > Should I extract the required parts of json object and create an another  
> > > json object for indexing? I think there should be a better way to do this.  
> > > Could anyone give my some advices? Thanks a lot!
> > 
> > --  
> > 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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).
> 
> --  
> 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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:** ![11122](https://avatars.discourse-cdn.com/v4/letter/1/6a8cbe/32.png) [@11122](https://discuss.elastic.co/u/11122)\
**Post date:** [July 2, 2013, 3:10pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/6 "2013-07-02T15:10:17Z")

</div>

I think I found the problem. the addField() can return a field which should  
be stored first, so I add field("store","yes") to the field I'd like to  
return.  
But I noticed if I setup my own mapping and just create a sample index and  
then use addField(), it works OK, I don't need to specify store in my  
mapping. In my case, I load a json file and setup a mapping for it in order  
to index only parts of this file, when I search this created index and want  
to get a certain field values, I should specify it has been set  
field("store","yes"). Why is it work in this way? Could anybody help my to  
understand this? Thanks very much!  
I'm so new to Elasticsearch, please forgive my stupid questions! 🙂

2013/7/2 Chang Zhang [chang.zhang29@googlemail.com](mailto:chang.zhang29@googlemail.com)

> Thank you.  
> now I noticed that my mapping has been working to the index, because when  
> I search the field which has been enabled false in the mapping cannot be  
> hitted. So this means that this part is not indexed？ do I understand  
> correctly?
> 
> but here is an another question.  
> I add a field to be loaded and return in the searchresponse with  
> addField("myfield"), it seems that there is no required field returned,  
> here is my code and a screenshot of debug result.
> 
> SearchResponse searchResponse = node.client().prepareSearch("pbl")  
> .setTypes("testpbl")  
> .setQuery(QueryBuilders.matchQuery("sid","12345"))  
> .addField("sid")  
> .setExplain(true)  
> .setSize(10)  
> .execute().actionGet();  
> [image: 内嵌图片 1]
> 
> 2013/7/2 David Pilato [david@pilato.fr](mailto:david@pilato.fr)
> 
> > Just a note to this: There is a big difference between indexing and  
> > storing source document.
> > 
> > Elasticsearch won't touch your source document and will store it as is.  
> > For example, if you use carriage return after each field, you will get it  
> > back when doing get or search operations.
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 2 juil. 2013 à 12:49, Chang Zhang [chang.zhang29@googlemail.com](mailto:chang.zhang29@googlemail.com) a  
> > écrit :
> > 
> > Thanks a lot.  
> > I've read something about mapping and notice there is a "enabled" flag(  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type/)). I  
> > tried to create an explicit mapping with this flag, here is my code:  
> > String mapping =  
> > XContentFactory.jsonBuilder().startObject().startObject("testpbl")  
> > .startObject("properties")  
> > .startObject("person").field("type", "object")  
> > .startObject("properties")  
> > .startObject("name").field("type", "object")  
> > .field("enabled", false)  
> > .endObject()  
> > .startObject("sid").field("type","string").endObject()  
> > .endObject()  
> > .startObject("message").field("type",  
> > "string").endObject()  
> > .endObject().endObject().endObject().endObject()  
> > .string();
> > 
> > ```
> > node.client().admin()
> > .indices().prepareCreate("pbl")
> > .addMapping("testpbl", mapping)
> > .execute().actionGet();
> > 
> > JSONParser parser = new JSONParser();
> > Object object = parser.parse(new FileReader("mydoc\\test.js"));
> > JSONObject jsonData = (JSONObject) object;
> > 
> > node.client().prepareIndex("pbl", "testpbl",
> > 
> > ```
> > 
> > "1").setSource(jsonData).execute().actionGet();
> > 
> > and the test.js is:
> > 
> > {  
> > "tweet": {  
> > "person": {  
> > "name": {  
> > "first\_name": "Shay",  
> > "last\_name": "Banon"  
> > },  
> > "sid": "12345"  
> > },  
> > "message": "This is a tweet!"  
> > }  
> > }
> > 
> > the question is, it seems that my mapping doesn't work. When I tried to  
> > get my created index and check its content, I still get the name's content  
> > which should not be indexed according to my mapping.
> > 
> > is there any error with my code? I will appreciate for helping.
> > 
> > 2013/7/2 ppearcy [ppearcy@gmail.com](mailto:ppearcy@gmail.com)
> > 
> > > Hi,  
> > > There are a couple of options...
> > > 
> > > The elasticsearch defaults are great for playing around, but it is  
> > > almost always a good idea to disable dynamic mapping and setup explicit  
> > > mappings([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to  
> > > configure what fields get indexed and which ones don't.
> > > 
> > > The next question is if you want to keep the original JSON or have  
> > > elasticsearch strip out elements you don't want. You can accomplish this  
> > > with source field include/excludes:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/source-field/)
> > > 
> > > Best Regards,  
> > > Paul
> > > 
> > > On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:
> > > 
> > > > I've parsed a json file into an JSONObject and created an index for  
> > > > this whole JSONObject.  
> > > > But how can I index only a part of this JSONObject, for instance, only  
> > > > several fields or nested parts?  
> > > > Should I extract the required parts of json object and create an  
> > > > another json object for indexing? I think there should be a better way to  
> > > > do this.  
> > > > Could anyone give my some advices? Thanks a lot!
> > > 
> > > --  
> > > 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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).
> > 
> > --  
> > 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/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:** ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)\
**Post date:** [July 2, 2013, 10:36pm UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/7 "2013-07-02T22:36:24Z")

</div>

Heya,  
This is a pretty good explanation:

> <https://stackoverflow.com/questions/17103047/why-do-i-need-storeyes-in-elasticsearch>

To summarize, there are two ways to store fields in elasticsearch.

- Store = yes where it is actually stored by lucene
- Stored as part of \_source. Here the single \_source field is stored and  
individual items are extracted via the JSON.

There are some nuances to both, but in general, I recommend only storing  
\_source vs other individual fields.

Best Regards,  
Paul

On Tuesday, July 2, 2013 9:10:17 AM UTC-6, Chang Zhang wrote:

> I think I found the problem. the addField() can return a field which  
> should be stored first, so I add field("store","yes") to the field I'd like  
> to return.  
> But I noticed if I setup my own mapping and just create a sample index and  
> then use addField(), it works OK, I don't need to specify store in my  
> mapping. In my case, I load a json file and setup a mapping for it in order  
> to index only parts of this file, when I search this created index and want  
> to get a certain field values, I should specify it has been set  
> field("store","yes"). Why is it work in this way? Could anybody help my to  
> understand this? Thanks very much!  
> I'm so new to Elasticsearch, please forgive my stupid questions! 🙂
> 
> 2013/7/2 Chang Zhang \<[chang....@googlemail.com](mailto:chang....@googlemail.com) \<javascript:\>\>
> 
> > Thank you.  
> > now I noticed that my mapping has been working to the index, because when  
> > I search the field which has been enabled false in the mapping cannot be  
> > hitted. So this means that this part is not indexed？ do I understand  
> > correctly?
> > 
> > but here is an another question.  
> > I add a field to be loaded and return in the searchresponse with  
> > addField("myfield"), it seems that there is no required field returned,  
> > here is my code and a screenshot of debug result.
> > 
> > SearchResponse searchResponse = node.client().prepareSearch("pbl")  
> > .setTypes("testpbl")  
> > .setQuery(QueryBuilders.matchQuery("sid","12345"))  
> > .addField("sid")  
> > .setExplain(true)  
> > .setSize(10)  
> > .execute().actionGet();  
> > [image: 内嵌图片 1]
> > 
> > 2013/7/2 David Pilato \<[da...@pilato.fr](mailto:da...@pilato.fr) \<javascript:\>\>
> > 
> > > Just a note to this: There is a big difference between indexing and  
> > > storing source document.
> > > 
> > > Elasticsearch won't touch your source document and will store it as is.  
> > > For example, if you use carriage return after each field, you will get  
> > > it back when doing get or search operations.
> > > 
> > > --  
> > > David 😉  
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > > 
> > > Le 2 juil. 2013 à 12:49, Chang Zhang \<[chang....@googlemail.com](mailto:chang....@googlemail.com)\<javascript:\>\>  
> > > a écrit :
> > > 
> > > Thanks a lot.  
> > > I've read something about mapping and notice there is a "enabled" flag(  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/object-type/)). I  
> > > tried to create an explicit mapping with this flag, here is my code:  
> > > String mapping =  
> > > XContentFactory.jsonBuilder().startObject().startObject("testpbl")  
> > > .startObject("properties")  
> > > .startObject("person").field("type", "object")  
> > > .startObject("properties")  
> > > .startObject("name").field("type", "object")  
> > > .field("enabled", false)  
> > > .endObject()  
> > > .startObject("sid").field("type","string").endObject()  
> > > .endObject()  
> > > .startObject("message").field("type",  
> > > "string").endObject()  
> > > .endObject().endObject().endObject().endObject()  
> > > .string();
> > > 
> > > ```
> > > node.client().admin()
> > > .indices().prepareCreate("pbl")
> > > .addMapping("testpbl", mapping)
> > > .execute().actionGet();
> > > 
> > > JSONParser parser = new JSONParser();
> > > Object object = parser.parse(new FileReader("mydoc\\test.js"));
> > > JSONObject jsonData = (JSONObject) object;
> > > 
> > > node.client().prepareIndex("pbl", "testpbl", 
> > > 
> > > ```
> > > 
> > > "1").setSource(jsonData).execute().actionGet();
> > > 
> > > and the test.js is:
> > > 
> > > {  
> > > "tweet": {  
> > > "person": {  
> > > "name": {  
> > > "first\_name": "Shay",  
> > > "last\_name": "Banon"  
> > > },  
> > > "sid": "12345"  
> > > },  
> > > "message": "This is a tweet!"  
> > > }  
> > > }
> > > 
> > > the question is, it seems that my mapping doesn't work. When I tried to  
> > > get my created index and check its content, I still get the name's content  
> > > which should not be indexed according to my mapping.
> > > 
> > > is there any error with my code? I will appreciate for helping.
> > > 
> > > 2013/7/2 ppearcy \<[ppe...@gmail.com](mailto:ppe...@gmail.com) \<javascript:\>\>
> > > 
> > > > Hi,  
> > > > There are a couple of options...
> > > > 
> > > > The elasticsearch defaults are great for playing around, but it is  
> > > > almost always a good idea to disable dynamic mapping and setup explicit  
> > > > mappings([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)) to  
> > > > configure what fields get indexed and which ones don't.
> > > > 
> > > > The next question is if you want to keep the original JSON or have  
> > > > elasticsearch strip out elements you don't want. You can accomplish this  
> > > > with source field include/excludes:  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/source-field/)
> > > > 
> > > > Best Regards,  
> > > > Paul
> > > > 
> > > > On Monday, July 1, 2013 9:56:17 AM UTC-6, 少爷允之 wrote:
> > > > 
> > > > > I've parsed a json file into an JSONObject and created an index for  
> > > > > this whole JSONObject.  
> > > > > But how can I index only a part of this JSONObject, for instance, only  
> > > > > several fields or nested parts?  
> > > > > Should I extract the required parts of json object and create an  
> > > > > another json object for indexing? I think there should be a better way to  
> > > > > do this.  
> > > > > Could anyone give my some advices? Thanks a lot!
> > > > 
> > > > --  
> > > > 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/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.
> > > 
> > > 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 a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/unsubscribe](https://groups.google.com/d/topic/elasticsearch/c7IQfT7Z8Os/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > 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:28am UTC](https://discuss.elastic.co/t/how-to-index-parts-of-json-files-using-java-api/12604/8 "2017-07-06T02:28:34Z")

</div>


