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