I want to find a phrase in a document, I've used the codes in the quick start.
>>> 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"}
But then I find they will split the keywords into several single word and then search the document. If I want to search a phrase like "the first guy here in the document", what should I do.
On the document ,it said, use
"it is a phrase"
if I want to search for:
it is a phrase.
That confuses me.
Besides, here is a class ,which seems can help me , but I don't know how to use it.
class whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None)
Matches documents containing a given phrase.
Update: I use it in this way, but there is no matches.
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
result:
Top 0 Results for Phrase('content', u'the first', slop=1, boost=1.000000) runtime=0.0>
Update according to theOther
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'}>
I think there should be no matched result ,as there is no "first x document" in the document. Otherwise, it is not an exact match.