Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No alias field #1477

Open
LasseGravesen opened this issue Jan 12, 2021 · 3 comments
Open

No alias field #1477

LasseGravesen opened this issue Jan 12, 2021 · 3 comments

Comments

@LasseGravesen
Copy link

An alias mapping defines an alternate name for a field in the index. The alias can be used in place of the target field in search requests.
https://www.elastic.co/guide/en/elasticsearch/reference/master/alias.html

It seems that this field is not included in elasticsearch-dsl currently.
I wanted to ask if anyone was working on adding this already, and if not could I open a PR as it would be simple to add.

@sandeshnaroju
Copy link

any update on this?

@Zacharis278
Copy link

Zacharis278 commented Jul 21, 2021

+1 I also have a need for this. Currently have a service doing a search with an alias target and as a result it won't map back to the correct python type due to strict matching on the returned _index. That index wouldn't match the index field (which is really the alias) on my Document.

@LasseGravesen
Copy link
Author

LasseGravesen commented Jul 22, 2021

Here's what I did:
in helpers.py

from elasticsearch7_dsl import Field

class Alias(Field):
    """Alias field.
    https://www.elastic.co/guide/en/elasticsearch/reference/master/alias.html
    """

    name = "alias"
    _coerce = True

    def to_dict(self):
        """Return as dict."""
        d = super(Alias, self).to_dict()
        d["type"] = "alias"
        return d

Then in the template file, template.py

from helpers import Alias
from elasticsearch7_dsl import Document, InnerDoc, Integer, MetaField, Object

class Test(InnerDoc):
    """Test inner document type."""

    xyz = Alias(path="test.abc")
    abc = Integer()

class TestDoc(Document):
    """TestDoc Document class."""

    test = Object(doc_class=Test)

    class Index:
        """Specify the index meta information."""

        name = "test-docs-*"

    class Meta:
        """Add strictness to schema, new fields cannot be arbitrarily added."""

        dynamic = MetaField("strict")

When can then be used to instantiate templates on the cluster:

from template import TestDoc

def create_template():
    """Create template."""
    mentions = TestDoc._index.as_template(
        "test-docs-template", order=0
    )


def main():
    """Runnable entry point."""
    connections.create_connection(
        hosts=["https://my-es-cluster:443/"],
        alias="default",
        timeout=20,
        http_auth="abc:def",
        ssl=True,
    )
    create_template()


if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants