Google App Engine - Using Search API Python with list fields

2024/10/6 12:26:01

I'm using ndb.Model. The Search API has the following field classes:

    TextField : plain textHtmlField : HTML formatted textAtomField : a string which is treated as a single tokenNumberField : a numeric value (either float or integer)DateField : a date with no time componentGeoField : a locale based on latitude and longitude

Suppose I have a 'tags' field which is a list field:

    tags = ndb.StringProperty(repeated=True)

How am I supposed to treat this field with search.Document?

Right now I'm turning tags list into a string:

    t = '|'.join(tags)

And then:

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

Any suggestions?

Answer

You should add as many fields as 'tags' you have, all with 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 field's type. Field names do not have to be unique. A document can have multiple fields with the same name and same type, which is a way to represent a field with multiple values. (However, date and number fields with the same name can't be repeated.) A document can also contain multiple fields with the same name and different field types.

https://en.xdnf.cn/q/70363.html

Related Q&A

Handling PyMySql exceptions - Best Practices

My question regards exception best practices. Ill present my question on a specific case with PyMySQL but it regards errors handling in general. I am using PyMySQL and out of the many possible exceptio…

Beautifulsoup find element by text using `find_all` no matter if there are elements in it

For examplebs = BeautifulSoup("<html><a>sometext</a></html>") print bs.find_all("a",text=re.compile(r"some"))returns [<a>sometext</a>] …

Python: how to get values from a dictionary from pandas series

I am very new to python and trying to get value from dictionary where keys are defined in a dataframe column (pandas). I searched quite a bit and the closest thing is a question in the link below, but…

Django No Module Named URLs error

There are many similar questions posted already, but Ive already tried those solutions to no avail. Im working through a basic Django tutorial, and here is my code:urls.pyfrom django.conf.urls import …

Matplotlib artist to stay same size when zoomed in but ALSO move with panning?

This is a very direct follow-up on this question.Using matplotlib, Id like to be able to place a sort of "highlighting bar" over a range of data markers that I know will all be in a straight …

How to invoke Lambda function with Event Invocation Type via API Gateway?

Docs says:By default, the Invoke API assumes RequestResponse invocation type. You can optionally request asynchronous execution by specifying Event as the InvocationType. So all I can send to my functi…

How do I unlock the app engine database when localhost runs?

Right now I get a blank page when localhost runs, but the deployed app is fine. The logs show the "database is locked". How do I "unlock" the database for localhost?

PyCrypto: Generate RSA key protected with DES3 password

I have been able to create a RSA key protected by password with DES3 (well... I think because Im very new to this encryption world) by using the command:openssl genrsa -out "/tmp/myKey.pem" -…

Normalize/Standardize a numpy recarray

I wonder what the best way of normalizing/standardizing a numpy recarray is. To make it clear, Im not talking about a mathematical matrix, but a record array that also has e.g. textual columns (such as…

How to read /dev/log?

I would like to directly access to syslog messages from Python by reading /dev/log.My (very limited) understanding is that the correct way is to read from there is to bind a datagram socket. import soc…