0

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?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
Koerr
  • 15,215
  • 28
  • 78
  • 108

1 Answers1

2

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?.)

Community
  • 1
  • 1
Mark Leighton Fisher
  • 5,609
  • 2
  • 18
  • 29
  • 1
    I've done this before, except I didn't use IDs, I just stored the name as a path eg `category=root_1/sub_1` etc and then searched for `root_1` using `category:root_1*` – Bohemian May 08 '12 at 13:01