Поиск отдельных полей терминов с помощью Lucene (PyLucene) - PullRequest
3 голосов
/ 01 февраля 2012

Я довольно новичок в терминах векторов Lucene - и хочу убедиться, что мой сбор терминов настолько эффективен, насколько это возможно. Я получаю уникальные термины, а затем извлекаю docFreq () для выполнения фасетирования.

Я собираю все термины документов из индекса, используя:

lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
terms = ireader.terms() #Returns TermEnum

Это прекрасно работает, но есть ли способ вернуть условия только для определенных полей (во всех документах) - не будет ли это более эффективным?

Например:

 ireader.terms(Field="country")

1 Ответ

3 голосов
/ 04 марта 2012

IndexReader.terms () принимает необязательный объект Field ().Объекты поля состоят из двух аргументов: имени поля и значения, которое lucene называет «полем термина» и «текстом термина».

Предоставляя аргумент поля с пустым значением для «текста текста», мыможет начать нашу итерацию термина с термина, который нас интересует.

lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
# Query the lucene index for the terms starting at a term named "field_name"
terms = ireader.terms(Term("field_name", "")) #Start at the field "field_name"
facets = {'other': 0}
while terms.next():
    if terms.term().field() != "field_name":  #We've got every value
        break
    print "Field Name:", terms.term().field()
    print "Field Value:", terms.term().text()
    print "Matching Docs:", int(ireader.docFreq(term))

Надеемся, что другие, ищущие способы выполнения огранки в PyLucene, увидят эту статью.Ключ индексирует условия как есть.Просто для полноты вот как следует индексировать значения полей.

dir = SimpleFSDirectory(File(indexdir))
analyzer = StandardAnalyzer(Version.LUCENE_30)
writer = IndexWriter(dir, analyzer, True, IndexWriter.MaxFieldLength(512))
print "Currently there are %d documents in the index..." % writer.numDocs()
print "Adding %s Documents to Index..." % docs.count()
for val in terms:
    doc = Document()
    #Store the field, as-is, with term-vectors.
    doc.add(Field("field_name", val, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES))
    writer.addDocument(doc)

writer.optimize()
writer.close()
...