Skip to content

Commit eb237fd

Browse files
committed
feat: add method to get field names on latest index
Useful for very quickly retrieving a basic list of all fields from the latest index. Returns field type information, but type counts are all set to 1.
1 parent a75c550 commit eb237fd

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

splitgill/manager.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from cytoolz.dicttoolz import get_in
66
from elasticsearch import Elasticsearch
7-
from elasticsearch_dsl import Search
7+
from elasticsearch_dsl import Index, Search
88
from elasticsearch_dsl.query import Query
99
from pymongo import ASCENDING, DESCENDING, IndexModel, MongoClient
1010
from pymongo.collection import Collection
@@ -735,3 +735,27 @@ def get_parsed_fields(
735735
# descending frequency (so most frequent fields first)
736736
parsed_fields.sort(key=lambda f: f.count, reverse=True)
737737
return parsed_fields
738+
739+
def get_field_names(self) -> List[str]:
740+
"""
741+
Retrieves a list of field names from the latest index mapping.
742+
743+
Does not take any version or query parameters; simply returns all the "data."
744+
fields available on the index, along with their available types. All relevant
745+
type counts are set to 1 to enable use of e.g. .is_text(). Use get_data_fields
746+
or get_parsed_fields if you need accurate counts, or to filter by version or
747+
query.
748+
"""
749+
latest_index = Index(self.indices.latest, using=self._client.elasticsearch)
750+
mapping = latest_index.get_mapping()
751+
parsed_fields = []
752+
for field_path, field_props in get_in(
753+
[self.indices.latest, 'mappings', 'properties', 'data', 'properties'],
754+
mapping.body,
755+
default={},
756+
).items():
757+
parsed_field = ParsedField(field_path)
758+
for type_name in field_props['properties'].keys():
759+
parsed_field.add(type_name, 1)
760+
parsed_fields.append(parsed_field)
761+
return parsed_fields

tests/test_manager.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,3 +1476,29 @@ def test_resync_arcs(splitgill: SplitgillClient):
14761476
assert count == 2400
14771477
assert r_5_count == 3
14781478
assert r_780_count == 2
1479+
1480+
1481+
def test_get_field_names(splitgill: SplitgillClient):
1482+
database = splitgill.get_database('test')
1483+
records = [
1484+
Record.new({'a': 1}),
1485+
Record.new({'a': 2}),
1486+
Record.new({'b': 3}),
1487+
Record.new({'b': 'x'}),
1488+
Record.new({'b': 5}),
1489+
Record.new({'c': 'y'}),
1490+
]
1491+
database.ingest(records, commit=True)
1492+
database.sync()
1493+
1494+
field_names = database.get_field_names()
1495+
assert len(field_names) == 4
1496+
expected_fields = [
1497+
pf('_id', 3, t=1),
1498+
pf('a', 4, t=1, n=1),
1499+
pf('b', 4, t=1, n=1),
1500+
pf('c', 3, t=1),
1501+
]
1502+
for f in expected_fields:
1503+
f.type_counts[ParsedType.UNPARSED] = 1
1504+
assert field_names == expected_fields

0 commit comments

Comments
 (0)