1

It is possible to create query that will search in all fields that have names with a given prefix?

I need that because I need to index some info that comes with an object that I'm indexing, e.g. category. Categories can have different levels. I want to give different boosts to different category levels.

I have noticed that I can add a field more than once while indexing. Can I just add this catName field multiple times, with different boost and then search it as it is was one field?

int level = 1;

for (Category cat : sortedCategories) {
    float boost = CATEGORY_BOOST / level;
    String catName = CATEGORY_NAME + String.valueOf(level);
    TextField categoryName = new TextField(catName, cat.getName(),
                                           Field.Store.NO);
    categoryName.setBoost(boost);
    doc.add(categoryName);
Damian
  • 2,930
  • 6
  • 39
  • 61

1 Answers1

1

You can leave the indexing unchanged, and just create a BooleanQuery that uses multiple clauses, one per field name, and Occurs.SHOULD.

Or, you can create a dedicated index for the prefix - if your field names are 'Pre1' and 'Pre2', you can add a new indexing field, 'Pre' and index the aggregate of documents from 'Pre1' and 'Pre2'.

Tim Lovell-Smith
  • 15,310
  • 14
  • 76
  • 93
  • But there can be Pre1 and Pre2 but also Pre6. So Using BooleanQuery does not seems elegant or flexible. But I am not sure how the second option can be implemented. I think I will create multiple fields with the same name, but with different boost. should work ?: http://stackoverflow.com/questions/401754/field-having-multiple-distinct-values – Damian Dec 01 '12 at 12:39
  • 'Pre1 and Pre2 but also Pre6' - not a problem, you can have any number of clauses in a BooleanQuery – Tim Lovell-Smith Dec 05 '12 at 19:13