Google App Engine - Using the Python Search API with List Fields

I am using ndb.Model. The search API has the following field classes:

TextField : plain text HtmlField : HTML formatted text AtomField : a string which is treated as a single token NumberField : a numeric value (either float or integer) DateField : a date with no time component GeoField : a locale based on latitude and longitude 

Suppose I have a tag field, which is a list box:

  tags = ndb.StringProperty(repeated=True) 

How should I view this field using search.Document ?

Now I turn the tags list into a string:

  t = '|'.join(tags) 

And then:

  search.TextField(name=cls.TAGS, value=t) 

Any suggestions?

+7
source share
2 answers

Use unique identifiers for each "tag". Then you can create a document, for example:

 doc = search.Document(fields=[ search.TextField(name='tags', value='tag1 tag2 tag3'), ]) search.Index(name='tags').put(doc) 

You can even use numbers (ids) as strings:

 doc = search.Document(fields=[ search.TextField(name='tags', value='123 456 789'), ]) 

And a query using the operators as you wish:

 index = search.Index(name='tags') results = index.search('tags:(("tag1" AND "tag2") OR ("tag3" AND "tag4"))') 
+6
source

You have to add as many fields as there are "tags", you have all the same field_name:

 doc = search.Document(fields=[ search.TextField(name='tag', value=t) for t in tags ]) 

As in the docs:

A field can contain only one value, which must match the type of field. Field names do not have to be unique. A document can have several fields with the same name and the same type, which allows you to represent a field with several values. (However, date and number fields with the same name cannot be repeated.) A document can also contain several fields with the same name and different field types.

+5
source

All Articles