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.

[Question] How to represent a list/array of strings in persistence models

See original GitHub issue

Hi, I need to represent this

PUT my_index/animal/4444
{

  "name": "Arara",
  "location": "Brasil",
  "colors": ["Yellow", "Green", "Blue"]
}

The above works nice and I can perform matches on colors

GET /myindexer/animal/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "colors": "Blue"
        }}
      ]
    }
  }
}

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.5,
      "hits": [
         {
            "_index": "my_index",
            "_type": "animal",
            "_id": "4444",
            "_score": 0.5,
            "_source": {
               "name": "Arara",
               "location": "Brasil",
               "colors": [
                  "Yellow",
                  "Green",
                  "Blue"
               ]
            }
         }
      ]
   }
}

So to represent the colors list how can I do, is there a List or Array type?


class Animal(DocType):
    name = String()
    location = String()
    colors = List(String()) (????)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
honzakralcommented, Apr 30, 2019

@abdelwahebmoalla you don’t need to explicitly define a field as a list, just assign a list of values to it and it will work. if you want the default value of that field to be a list just specify multi=True when instantiating the field on your Document subclass:

from elasticsearch_dsl import Document, Keyword, Text

class BlogPost(Document):
    title = Text()
    content = Text()
    tags = Keyword(multi=True)

BlogPost.init()
bp = BlogPost()
bp.tags.append('tag1')
bp.save()

Hope this helps!

1reaction
honzakralcommented, Mar 10, 2015

I want to implement a switch to make a field a list by default. It will be available for all the fields. It was described in #83

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to persist a property of type List<String> in JPA?
Command.java. package persistlistofstring; import java.io.Serializable; import java.util.ArrayList; import ...
Read more >
Mapping Arrays with Hibernate
Databases support arrays and it's a common Java type. But mapping it with Hibernate is more complex than it might seem.
Read more >
String Array in Java with Examples
This code constructs a new ArrayList based on the value returned by Arrays.asList. It will not raise an exception. Converting String Array To...
Read more >
Convert an ArrayList of String to a String Array in Java
Method 1: Using ArrayList.get() Method of ArrayList class · Get the ArrayList of Strings. · Find the size of ArrayList using size() method,...
Read more >
Hibernate ORM 5.2.18.Final User Guide
In JPA nomenclature, the Session is represented by an EntityManager . ... static final Map<String,String> ABBREVIATIONS = buildAbbreviationMap(); @Override ...
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