python - 精确匹配一个词组

标签 python whoosh

我想在文档中查找短语,我已经使用了快速入门中的代码。

>>> from whoosh.index import create_in
>>> from whoosh.fields import *
>>> schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
>>> ix = create_in("indexdir", schema)
>>> writer = ix.writer()
>>> writer.add_document(title=u"First document", path=u"/a", content=u"This is the first document we've added!")
>>> writer.add_document(title=u"Second document", path=u"/b",  content=u"The second one is even more interesting!")
>>> writer.commit()
>>> from whoosh.qparser import QueryParser
>>> with ix.searcher() as searcher:
        query = QueryParser("content", ix.schema).parse("first")
        results = searcher.search(query)
        results[0]

    result: {"title": u"First document", "path": u"/a"}

但后来我发现他们会将关键字拆分成几个单独的词,然后搜索文档。 如果我想搜索像“文档中的第一个人”这样的短语,我应该怎么做。

文档上说,使用

"it is a phrase"

如果我想搜索:

it is a phrase.

这让我很困惑。

此外,这里有一个类,似乎可以帮助我,但我不知道如何使用它。

class whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None)
 Matches documents containing a given phrase.

更新: 我是这样用的,没有匹配到。

from whoosh.index import create_in
from whoosh.fields import *
schema = Schema(title=TEXT(stored=True), path=ID(stored=True),   content=TEXT)
ix = create_in("indexdir", schema)
writer = ix.writer()
writer.add_document(title=u"First document", path=u"/a",
                 content=u"This is the first document we've added!")
writer.add_document(title=u"Second document", path=u"/b",
               content=u"The second one is even more interesting!")
writer.commit()
from whoosh.query import Phrase

a = Phrase("content", u"the first")

results = ix.searcher().search(a)
print results

结果:

Top 0 Results for Phrase('content', u'the first', slop=1, boost=1.000000) runtime=0.0>

根据其他更新

with ix.searcher() as searcher:
    query = QueryParser("content", ix.schema).parse(**'"first x document"'**)
results = searcher.search(query)
print results[0]

result : Hit {'content': u"This is the first document we've added!", 'path': u'/a', 'title': u'First document'}>

我认为应该没有匹配的结果,因为文档中没有“first x document”。否则,它不是精确匹配。

最佳答案

你应该给 Phrase 一个 list 的单词而不是一个字符串作为第二个参数,同时去掉 the 因为它是一个停用词:

a = Phrase("content", [u"first",u"document"])

代替

a = Phrase("content", u"the first")

阅读文档:

class whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None)
Matches documents containing a given phrase.

Parameters:

fieldname – the field to search.

words – a list of words (unicode strings) in the phrase.

在 whoosh 中短语搜索的自然用法是在 QueryParser 中使用 Quotes "":

>>> with ix.searcher() as searcher:
        query = QueryParser("content", ix.schema).parse('"first document"')
        results = searcher.search(query)
        results[0]

更新:对于"first x document"匹配的是什么,因为x和所有单字词都是停用词并被过滤。

关于python - 精确匹配一个词组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251951/

相关文章:

python - 从父类(super class)获取子类的名称?

python - 对具有相同名称的行进行分组的最佳方法

python - Flask 应用程序搜索栏

python - django haystack Whoosh - SearchQuerySet().models(ModelName) 没有缩小结果范围

python - 通知父实例有关属性更改

Python:如何解决 python setup.py egg_info failed with error code 1

Python-ldap ldap.initialize 拒绝 ldapurl 认为有效的 URL

python - 嗖嗖功能

python - 如何获取 Whoosh 索引中所有术语的列表?

python - Whoosh - 访问 search_page 结果项抛出 ReaderClosed 异常