Get the field names where the search query is found


(Sanjay Reddy) #1

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


(Thomas Decaux) #2

Did you try highlighthing?

https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html


(Jimferenczi) #3

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
The API allows you to filter (per index and per field) the mapping information you need.


(Sanjay Reddy) #4

Hi,
Thanks for the quick reply :slight_smile:

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


(Sanjay Reddy) #5

Hi,
Thanks for the quick reply :slight_smile:

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???


(Jimferenczi) #6

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.


(Sanjay Reddy) #7

I'll try it out :slight_smile:

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


(Jimferenczi) #8

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


(system) #9