Elasticsearch-dsl-py: How can I implement copy_to?

Created on 4 Oct 2018  路  4Comments  路  Source: elastic/elasticsearch-dsl-py

How can I use elasticsearch's copy_to feature?

Is there anything like the following?

class NameDoc(Document):
    """
    Elastic document definition for name.
    """
    full_name = Text()
    first_name = Text(copy_to=full_name)
    last_name = Text(copy_to=full_name)

Most helpful comment

you just need to add quotes in the copy_to definition:

class NameDoc(Document):
    """
    Elastic document definition for name.
    """
    full_name = Text()
    first_name = Text(copy_to="full_name")
    last_name = Text(copy_to="full_name")

Hope this helps!

All 4 comments

you just need to add quotes in the copy_to definition:

class NameDoc(Document):
    """
    Elastic document definition for name.
    """
    full_name = Text()
    first_name = Text(copy_to="full_name")
    last_name = Text(copy_to="full_name")

Hope this helps!

Didn't work. This is what the mapping looks like:

"mappings": {

    "doc": {
        "properties": {
            "last_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "first_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            }
        }
    }

}

This is my code:

import elasticsearch
from elasticsearch_dsl import Document, Text, Index

index_name = "test4"
es6_client = elasticsearch.Elasticsearch("localhost:9200")


class NameDoc(Document):
    full_name = Text()
    first_name = Text(copy_to="full_name")
    last_name = Text(copy_to="full_name")


def start():
    test_index = Index(index_name)
    test_index.create(using=es6_client)


def index():
    names = [("John", "Legend"), ("Henry", "Miller"), ("John", "Miller"), ("Henry", "Legend")]
    for firstName, lastName in names:
        doc = NameDoc()
        doc.meta.id = "{}{}".format(firstName[0], lastName[0])
        doc.first_name = firstName
        doc.last_name = lastName
        doc.save(using=es6_client, index=index_name)


if __name__ == "__main__":
    start()
    index()

How did you create the mappings? When I use your NameDoc definiton to create the index I can see the copy_to fields being properly set.

Thanks!

Ah! missing the init. Thanks for pointing to the right direction

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vmogilev picture vmogilev  路  4Comments

barseghyanartur picture barseghyanartur  路  4Comments

quasiben picture quasiben  路  4Comments

leoliuxd picture leoliuxd  路  4Comments

rokcarl picture rokcarl  路  4Comments