Koerr
Koerr

Reputation: 15723

Using BooleanQuery or write more indexes?

A category tree like this:

root_1
  sub_1
  sub_2
  ... to sub_20 

Every document has a sub category(like sub_2). Now, I only wrote sub_2 in lucene index:

new NumericField("category",...).setIntValue(sub_2.getID());

I want to get all root_1's documents, using BooleanQuery (merge the sub_1 to sub_20) to search or write an other category in every entry document:

new NumericField("category",...).setIntValue(sub_2.getID());
new NumericField("category",...).setIntValue(root_1.getID());//sub_2's ancestor category

Which is the better choice?

Upvotes: 0

Views: 409

Answers (1)

Mark Leighton Fisher
Mark Leighton Fisher

Reputation: 5693

I would use a path enumeration/'Dewey Decimal' representation of the category hierarchy. That is, instead of just storing 'sub_2' for the second child of the first root, store instead something like '001.002'.

To find the root and all of its children, you would search on "category:001*".

To find only the children of the root, you would search on "category:001.*".

(Please also see How to store tree data in a Lucene/Solr/Elasticsearch index or a NoSQL db?.)

Upvotes: 2

Related Questions