0

I'm using Lucene 6.2.0 and trying to search over all the fields in a Document, without sending all field names. I've seen these answers, but it seems to me that these methods no longer exist in my version.

Community
  • 1
  • 1
superDoss
  • 61
  • 6

1 Answers1

0

The top answers in the question you linked to are all still largely valid.

I would recommend you index an "äll" field with the document. This is a pretty well-known pattern (Elasticsearch does this by default, in fact). Just create a indexed, analyzed, and not stored field, and add all of the searchable fields to it as well.

doc.add(new TextField("field1", fieldContent, Field.Store.YES));
doc.add(new TextField("_all", fieldContent, Field.Store.NO));

doc.add(new TextField("field2", anotherField, Field.Store.YES));
doc.add(new TextField("_all", anotherField, Field.Store.NO));

//etc.

I noticed you've tagged solr. If you are using Solr, you can use copyFields for this.

This approach is definitely what I would recommend. It will generally provide the best search performance, as well as being easy to use. You can just make it your QueryParser default field, or use it in a simple term query, etc.

Alternatively (if you don't want to reindex, or some such), you can indeed use MultiFieldQueryParser, as long as you are able to generate the list of all the field names you want to search:

QueryParser parser = new MultiFieldQueryParser(
    new String[] {"field1", "field2" /*etc.*/}, 
    myAnalyzer);
Query query = parser.parse("the query");
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • You've just wrote down the same answer from my reference answer, not really helpful... , anyway in my case i need to be able to search on a specific field, with the ability to youse complex queries like : AND, OR. Searching over all the text in one field, will get faulty results. – superDoss Sep 16 '16 at 06:36
  • @user5116821 So, I provided an up to date, complete answer, addressing standard (java) Lucene, rather than Lucene.Net... And that isn't helpful, because it bears some rough similarity to another couple answers, that you stated in your question that you don't think are still applicable? As far as your new question here, why did you ask how to search over all fields, if that isn't what you want to do? – femtoRgon Sep 16 '16 at 07:03