I'm making a search index which must support nested hierarchies of data. For test purposes, I'm making a very simple schema:
test_schema = Schema(
name_ngrams=NGRAMWORDS(minsize=4, field_boost=1.2),
name=TEXT(stored=True),
id=ID(unique=True, stored=True),
type=TEXT
)
For test data I'm using these:
test_data = [
dict(
name=u'The Dark Knight Returns',
id=u'chapter_1',
type=u'chapter'),
dict(
name=u'The Dark Knight Triumphant',
id=u'chapter_2',
type=u'chapter'),
dict(
name=u'Hunt The Dark Knight',
id=u'chapter_3',
type=u'chapter'),
dict(
name=u'The Dark Knight Falls',
id=u'chapter_4',
type=u'chapter')
]
parent = dict(
name=u'The Dark Knight Returns',
id=u'book_1',
type=u'book')
I've added to the index all the (5) documents, like this
with index_writer.group():
index_writer.add_document(
name_ngrams=parent['name'],
name=parent['name'],
id=parent['id'],
type=parent['type']
)
for data in test_data:
index_writer.add_document(
name_ngrams=data['name'],
name=data['name'],
id=data['id'],
type=data['type']
)
So, to get all the chapters for a book, I've made a function which uses a NestedChildren search:
def search_childs(query_string):
os.chdir(settings.SEARCH_INDEX_PATH)
# Initialize index
index = open_dir(settings.SEARCH_INDEX_NAME, indexname='test')
parser = qparser.MultifieldParser(
['name',
'type'],
schema=index.schema)
parser.add_plugin(qparser.FuzzyTermPlugin())
parser.add_plugin(DateParserPlugin())
myquery = parser.parse(query_string)
# First, we need a query that matches all the documents in the "parent"
# level we want of the hierarchy
all_parents = And([parser.parse(query_string), Term('type', 'book')])
# Then, we need a query that matches the children we want to find
wanted_kids = And([parser.parse(query_string),
Term('type', 'chapter')])
q = NestedChildren(all_parents, wanted_kids)
print q
with index.searcher() as searcher:
#these results are the parents
results = searcher.search(q)
print "number of results:", len(results)
if len(results):
for result in results:
print(result.highlights('name'))
print(result)
return results
But for my test data, if I search for "dark knigth", I'm only getting 3 results when it must be 4 search results.
I don't know if the missing result is excluded for having the same name as the book, but it's simply not showing in the search results
I know that all the items are in the index, but I don't know what I'm missing here.
Any thoughts?