Exemplo n.º 1
0
## 查询方法一:
## __init__(self, fieldname, schema, plugins=None)
# A hand-written query parser built on modular plug-ins. The default configuration implements a powerful fielded query language similar to Lucene.
# fieldname: the default field -- the parser uses this as the field for any terms without an explicit field.
# schema: a :class:`whoosh.fields.Schema` object to use when parsing. The appropriate fields in the schema will be used to
#     tokenize terms/phrases before they are turned into query objects.
#     You can specify None for the schema to create a parser that does not analyze the text of the query, usually for testing purposes.
parser = QueryParser("content", ix.schema)  # ix.schema 和 schema 是相同的东西
print(len(parser.plugins), parser.plugins)  # 11
# [<whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.SingleQuotePlugin>,
#  <whoosh.qparser.plugins.FieldsPlugin>,     <whoosh.qparser.plugins.WildcardPlugin>,   <whoosh.qparser.plugins.PhrasePlugin>,
#  <whoosh.qparser.plugins.RangePlugin>,      <whoosh.qparser.plugins.GroupPlugin>,      <whoosh.qparser.plugins.OperatorsPlugin>,
#  <whoosh.qparser.plugins.BoostPlugin>,      <whoosh.qparser.plugins.EveryPlugin>]
## default_set(): Returns the default list of plugins to use.
print(len(parser.default_set()), parser.default_set())  # 10
# [<whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.SingleQuotePlugin>, <whoosh.qparser.plugins.FieldsPlugin>,
#  <whoosh.qparser.plugins.WildcardPlugin>,   <whoosh.qparser.plugins.PhrasePlugin>,      <whoosh.qparser.plugins.RangePlugin>,
#  <whoosh.qparser.plugins.GroupPlugin>,      <whoosh.qparser.plugins.OperatorsPlugin>,   <whoosh.qparser.plugins.BoostPlugin>,
#  <whoosh.qparser.plugins.EveryPlugin>]
parser.remove_plugin_class(whoosh.qparser.plugins.WildcardPlugin)
print(len(parser.plugins), len(parser.default_set()))  # 10 10
parser.add_plugin(qparser.PrefixPlugin)
print(len(parser.plugins), len(parser.default_set()))  # 11 10
## parse(text, normalize=True, debug=False) Parses the input string and returns a :class:`whoosh.query.Query` object/tree.
query = parser.parse('document')
## search(q, **kwargs) Runs a :class:`whoosh.query.Query` object on this searcher and returns a :class:`Results` object.
# See :doc:`/searching` for more information.
results = searcher.search(query)  # 检索 "content" 中出现 "document"
print(
    results
Exemplo n.º 2
0
## 查询方法一:
## __init__(self, fieldname, schema, plugins=None)
# A hand-written query parser built on modular plug-ins. The default configuration implements a powerful fielded query language similar to Lucene.
# fieldname: the default field -- the parser uses this as the field for any terms without an explicit field.
# schema: a :class:`whoosh.fields.Schema` object to use when parsing. The appropriate fields in the schema will be used to
#     tokenize terms/phrases before they are turned into query objects.
#     You can specify None for the schema to create a parser that does not analyze the text of the query, usually for testing purposes.
parser = QueryParser("content", ix.schema)  # ix.schema 和 schema 是相同的东西
print(len(parser.plugins), parser.plugins)  # 11
# [<whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.SingleQuotePlugin>,
#  <whoosh.qparser.plugins.FieldsPlugin>,     <whoosh.qparser.plugins.WildcardPlugin>,   <whoosh.qparser.plugins.PhrasePlugin>,
#  <whoosh.qparser.plugins.RangePlugin>,      <whoosh.qparser.plugins.GroupPlugin>,      <whoosh.qparser.plugins.OperatorsPlugin>,
#  <whoosh.qparser.plugins.BoostPlugin>,      <whoosh.qparser.plugins.EveryPlugin>]
## default_set(): Returns the default list of plugins to use.
print(len(parser.default_set()), parser.default_set())  # 10
# [<whoosh.qparser.plugins.WhitespacePlugin>, <whoosh.qparser.plugins.SingleQuotePlugin>, <whoosh.qparser.plugins.FieldsPlugin>,
#  <whoosh.qparser.plugins.WildcardPlugin>,   <whoosh.qparser.plugins.PhrasePlugin>,      <whoosh.qparser.plugins.RangePlugin>,
#  <whoosh.qparser.plugins.GroupPlugin>,      <whoosh.qparser.plugins.OperatorsPlugin>,   <whoosh.qparser.plugins.BoostPlugin>,
#  <whoosh.qparser.plugins.EveryPlugin>]
parser.remove_plugin_class(whoosh.qparser.plugins.WildcardPlugin)
print(len(parser.plugins), len(parser.default_set()))  # 10 10
parser.add_plugin(qparser.PrefixPlugin)
print(len(parser.plugins), len(parser.default_set()))  # 11 10
## parse(text, normalize=True, debug=False) Parses the input string and returns a :class:`whoosh.query.Query` object/tree.
query = parser.parse('document')
## search(q, **kwargs) Runs a :class:`whoosh.query.Query` object on this searcher and returns a :class:`Results` object.
# See :doc:`/searching` for more information.
results = searcher.search(query)  # 检索 "content" 中出现 "document"
print(results)  # <Top 1 Results for Term('content', 'document') runtime=0.0015511049998622184>
print(type(results))  # <class 'whoosh.searching.Results'>