Edd
Edd

Reputation: 8570

How to search across multiple fields in Lucene using Query Syntax?

I'm searching a lucene index and I'm building search queries like

field1:"hello" AND field2:"world"

but I'd like to search for a value in any field as well as the values in specific fields in the same query i.e.

field1:"hello" AND anyField:"world"

Can anyone tell me how I can search across all indexed fields in this way?

Upvotes: 7

Views: 9828

Answers (3)

Erik Bergman
Erik Bergman

Reputation: 1

This might not apply to you, but in Azure Search, which is based on Lucene, using Lucene syntax, I use this:

name:plywood^100 OR plywood

Results with "plywood" in the "name" field are boosted.

Upvotes: 0

Edd
Edd

Reputation: 8570

Based on the answers I got for this question: Impact of repeat value across multiple fields in Lucene...

I can put the same search term into multiple fields and therefore create an "all" field which I put everything in. This way I can create a query like...

field1:"hello" AND all:"world"

This seems to work very nicely, prevents the need for huge search queries, and apparently the performance impact is minimal.

Upvotes: 7

A. Coady
A. Coady

Reputation: 57308

Boolean (OR) queries with a clause for each field are used to search multiple fields. The MultiFieldQueryParser will do that as well, but the fields still need to be enumerated. There's no implicit "all" fields; but IndexReader.getFieldNames can acquire them.

Upvotes: 2

Related Questions