question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting field names

See original GitHub issue

TL;DR: Is there any “simple” way to get document field names?

Despite ES being schemaless, I believe that many people works with ES respecting schema (which is pretty natural, as we can use object mapping). Personally, I try to verify any data on app side, to avoid implicit mapping, which can lead to troubles in future indexing/migrations. And as ES have no “constraints” against dynamic mapping and elasticsearch-dsl-py allows to save docs via DocType(**data) unpacking, I’ve extended DocType with

@classmethod
    def get_field_names(cls, path=None):
        if path is None:
            return cls._doc_type.mapping.properties.properties._d_.keys()
        else:
            getter = attrgetter('.'.join('{}.properties'.format(f) for f in path.split('.') if f))
            try:
                return getter(cls._doc_type.mapping.properties.properties)._d_.keys()
            except AttributeError:
                return []

which acts like Django’s get_all_field_names, but also allows to get field names for Nested and Object fields. Is there a simpler way, or such usecase is pretty rare? Thx.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
honzakralcommented, Apr 28, 2015

ES isn’t really schema-less, that is a common misconception based on it having a dynamic schema. It does have one. Also it has constraints against dynamic mapping - you can set dynamic to false (causing es to ignore unmapped fields) or you can set it to strict (causing ES to raise an error on encountering unmapped field) [0]. This made me realize that we don’t have a way to set this in the python dsl (created #146 to track.)

With that information I don’t think we need a get_field_by_name, or get_fields. But if you think it would be useful and others agree we can definitely add these method to DocTypeOptions where I think these should live.

Does this make sense?

Thanks

0 - http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-dynamic-mapping.html

0reactions
honzakralcommented, Jul 13, 2015

This has been resolved by e1bf242

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get name of a field - java - Stack Overflow
Show activity on this post. To get field's name, go like that: //use the field object from the example above field. getName(); //...
Read more >
MATLAB fieldnames - MathWorks
fields = fieldnames( S ) returns the field names of the structure array S in a cell array. fields = fieldnames( obj ,'-full')...
Read more >
How to get column names in Pandas dataframe
Let's discuss how to get column names in Pandas dataframe. First, let's create a simple dataframe with nba.csv file.
Read more >
How to get all field names of a table? - ServiceNow Community
Hi, You can go to System Definition -> Dictionary. Here you can type the table name and find all the related field names....
Read more >
How do I get all field names for an object in APEX?
system.debug('fieldName->>'+fieldName); // This will give you the api name of the field name. } Thanks. January 9, 2019; · ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found