Skip to content

Commit 1adbbdf

Browse files
committed
feat: add get_index_fields method to CogStack
Implement get_index_fields in pat2vec/pat2vec_search/cogstack_search_methods.py to retrieve all unique field names for aerty keys, and returns them as a sorte given Elasticsearch index or pattern. This method queries the index mapping, extracts propd list. Error handling for NotFoundError and general exceptions is included to ensure robustness.
1 parent 27f282d commit 1adbbdf

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

pat2vec/pat2vec_search/cogstack_search_methods.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,39 @@ def cogstack2df(
207207
df = pd.DataFrame(temp_results)
208208
return df
209209

210+
def get_index_fields(self, index_name: str) -> List[str]:
211+
"""Retrieves a list of all unique field names for a given
212+
Elasticsearch index or index pattern.
213+
214+
Args:
215+
index_name: The name of the index or an index pattern (e.g., 'my-index-*').
216+
217+
Returns:
218+
A sorted list of unique field names found across the matching indices.
219+
"""
220+
try:
221+
# Get the mapping for the given index or index pattern
222+
mapping = self.elastic.indices.get_mapping(index=index_name)
223+
224+
all_fields = set()
225+
226+
# Iterate through each index returned in the mapping
227+
for index_data in mapping.values():
228+
# The properties dictionary contains the field mappings
229+
properties = index_data.get("mappings", {}).get("properties", {})
230+
all_fields.update(properties.keys())
231+
232+
return sorted(list(all_fields))
233+
234+
except elasticsearch.exceptions.NotFoundError:
235+
logging.error(f"Index or pattern '{index_name}' not found.")
236+
return []
237+
except Exception as e:
238+
logging.error(
239+
f"An error occurred while fetching fields for index '{index_name}': {e}"
240+
)
241+
return []
242+
210243
def DataFrame(self, index: str) -> ed.DataFrame:
211244
"""Returns an Eland DataFrame for the specified index.
212245

0 commit comments

Comments
 (0)