svpino
svpino

Reputation: 1994

How to specify multiple search fields with the same name in Google App Engine Search API

So here's an interesting problem that I couldn't manage to solve on my own.

I'm setting up a search index where I want my documents to contain several AtomField representing categories. Each document can have more than one category.

Google's documentation says that a Document can be setup with multiple fields using the same name, which is awesome, however, you can only specify the fields using the constructor, and you can't modify the field list after the Document is created.

The question is, how can I create the Document object with one field per category assuming my categories are in an array? I saw this answer in another question here in Stack Overflow (can't find it now), but this doesn't work correctly:

document = search.Document(
    doc_id = files[file_index],
    fields=[
        search.TextField(name='name', value=my_name),
        search.AtomField(name='category', value=c) for c in categories
    ])

I think the challenge here is more related with Python than with App Engine per se.

If the problem is not clear, I wrote a blog post with more details.

Upvotes: 0

Views: 731

Answers (2)

Lasse
Lasse

Reputation: 86

I'm not sure this is what you're asking for, but it sounds like you just want to be able to concatenate two lists:

document = search.Document(
    doc_id = files[file_index],
    fields =
        [ search.TextField(name='name', value=my_name) ]
        + [ search.AtomField(name='category', value=c) for c in categories ]
    )

Upvotes: 2

Alan
Alan

Reputation: 690

You can include more than one instance of an AtomField with a given name in the fields array:

document = search.Document(
  doc_id = article["id"],
    fields=[
      search.TextField(name='title', value=article["title"]),
      search.DateField(name='date', value=article["date"]),                 
      search.HtmlField(name='content', value=article["html"]),
      search.AtomField(name='tag', value='Foo'),
      search.AtomField(name='tag', value='Bar')
    ])

Upvotes: 0

Related Questions