Field names with minus character

20 views
Skip to first unread message

Benjamin Behringer

unread,
Jun 18, 2013, 11:23:29 AM6/18/13
to who...@googlegroups.com
Hi,

I'm auto-generating fields in a schema using slugs as names. I recently added a field, which slug contained a minus character. Now the search with that field doesn't work anymore with the default QueryParser:

searchstring = 'foo f-bar:True'

query
= QueryParser('caption', ix.schema).parse(searchstring)
print(query)

The result is:

(caption:foo AND caption:f AND bar:t)

But I want to have:

(caption:foo AND f-bar:t)

Is there a way to achieve this without building the query by hand?

Regards,
Benjamin

Matt Chaput

unread,
Jun 18, 2013, 12:02:01 PM6/18/13
to who...@googlegroups.com
On 6/18/2013 11:23 AM, Benjamin Behringer wrote:
> Hi,
>
> I'm auto-generating fields in a schema using slugs as names. I recently
> added a field, which slug contained a minus character. Now the search
> with that field doesn't work anymore with the default QueryParser:

The default regular expression for field names doesn't include the '-'
character. Try this:


from whoosh import qparser

# Instantiate a query parser
qp = qparser.QueryParser("caption", ix.schema)

# Make a new Fields plugin with a more liberal regex
# (The regex must put the field name text in a "text" named group
# and it should allow "*" as a field name)
fp = qparser.FieldsPlugin(r"(?P<text>[-A-Za-z0-9]+|[*]):")

# Use the replace_plugin() method to replace the default Fields
# plugin with your new instance
qp.replace_plugin(fp)

qp.parse("foo f-bar:True")
# And([Term('caption', u'foo'), Term(u'f-bar', u'True')])


When coding I've always thought of field names as restricted to valid
Python identifiers, so I hope you don't run in to more problems with
your approach :) If you do, let me know and I'll see if they're fixable.

Cheers,

Matt

Reply all
Reply to author
Forward
Message has been deleted
0 new messages