# Get the field names where the search query is found

**URL:** <https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443>\
**Category:** Elasticsearch\
**Created:** [December 17, 2015, 8:09am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443 "2015-12-17T08:09:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)\
**Post date:** [December 17, 2015, 8:09am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/1 "2015-12-17T08:09:06Z")

</div>

Hi,

I got stuck somewhere in the middle. Please help me out

I have an index "sanjay\_info\_idx", in which the sample record is given below.  
{  
" **\_index**": "sanjay\_info\_idx",  
" **\_type**": "sanjay\_info\_idx",  
" **\_id**": "AVGmCwSaTP6SQFpi0uP0",  
" **\_version**": 1,  
" **\_score**": 1,  
" **\_source"** : {  
" **contentLen**": 1024,  
" **studentID**": "163",  
" **admissionID**": "55011",  
" **StudentName**": "RASHI KHANNA",  
" **gender**": "F",  
" **FacultyName**": "SARIN",  
" **remarks**": " Now we have the mappings, we can iterate the map to get the metadata. What we must be aware is that the structure is always the same whatever the specified parameters (index name, index type). This means that we need to browse the mapping structure in the same way. Following code describes how to get the mapping of types within a particular index ",  
" **suggestions**": "This means that we need to browse the mapping structure in the same way. Following code describes how to get the mapping of types within a particular index mappings",  
" **theme**": "What we must be aware is that the structure is always the same whatever the specified parameters mappings",  
"subjects": {  
"Maths": "yes",  
"English": "yes",  
"Social": "yes"  
}  
}  
}

I have two things to achieve:

1. How to get all the field names in the index like ["\_index","\_type","\_id","\_version","\_score","\_source","contentLen","studentID","admissionID","StudentName","gender","FacultyName","remarks","suggestions","theme","subjects","Maths","English","Social"]

2. When I search for a string " **mapping**", I want to get all the field names where the search word mapping is found i.e, [remarks, suggestions, theme]. Is it possible. If yes, please guide me

Thanks & Regards,  
Sanjay

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [December 17, 2015, 9:04am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/2 "2015-12-17T09:04:13Z")

</div>

Did you try highlighthing?

[https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html)

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [December 17, 2015, 9:08am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/3 "2015-12-17T09:08:01Z")

</div>

For 1):  
If you want to access the mapping of your index check this page:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html)  
The API allows you to filter (per index and per field) the mapping information you need.

---

<div class="post-metadata">

**Author:** ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)\
**Post date:** [December 17, 2015, 9:09am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/4 "2015-12-17T09:09:34Z")

</div>

Hi,  
Thanks for the quick reply 🙂

I'm trying to get the fields from java API. Is it possible to get from java ???

---

<div class="post-metadata">

**Author:** ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)\
**Post date:** [December 17, 2015, 9:16am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/5 "2015-12-17T09:16:10Z")

</div>

Hi,  
Thanks for the quick reply 🙂

I'm trying to get the fields from java API.

Currently I'm using the following code to access the mapping and get all the fields

SearchResponse res = req.execute().actionGet();  
for (SearchHit hit : res.getHits().getHits()) {  
for ( String key : hit.getSource().keySet() ) {

```
		    order.add(key);
		}
		resList.add(hit.getSource());
	}
	System.out.println(new Gson().toJson(order));

```

}

But the problem is I'm getting all the fields except  
"subjects": {  
"Maths": "yes",  
"English": "yes",  
"Social": "yes"  
}

Here I'm getting only subjects as field. But I want Maths, English , Social also as the field names. Is there anything that I have to change in the code???

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [December 17, 2015, 9:34am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/6 "2015-12-17T09:34:59Z")

</div>

In your example, you're not accessing the mapping per say, what you get is the \_source of the returned hits. The source is a Map where the keys are Strings and the values are Objects, the key set you're getting is only the first level of field names in your search hit. The 'subjects' fields in your type is an object, this means that it's an inner map inside the source. If you want to access all the fields you'll need to check the values in the map as well and if the value is an instance of Map you'll have to check all the key/value inside it and so on. A recursive function should do the trick.

---

<div class="post-metadata">

**Author:** ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)\
**Post date:** [December 17, 2015, 9:39am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/7 "2015-12-17T09:39:32Z")

</div>

I'll try it out 🙂

Is there any other possibility to get all the field names. Because the "subject" field may vary based on the student ID. So I need all the fields in the index. Can I get it using java code???  
If yes, please guide me how to do it ...

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [December 17, 2015, 9:52am UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/8 "2015-12-17T09:52:34Z")

</div>

The best way to get all the field names is to check the mapping of your index. You can try something like:

```
GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices("indexName");
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> = client.admin().indices().getMappings(GetMappingRequest).actionGet().getMappings();

```

Check this page to understand the format of the map you get in the response:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html)

---

<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 5, 2017, 11:30pm UTC](https://discuss.elastic.co/t/get-the-field-names-where-the-search-query-is-found/37443/9 "2017-07-05T23:30:16Z")

</div>


