Logical operators are called instead of OR because of the names that they have in the Lucene API and documentation, and because it is more appropriate: this not only affects the logical decision, but also affects the result of the result.
For example, if you are looking for Fiat or blue cars, cars branded Fiat & blue will also be returned and have a higher score than blue but not Fiat cars.
This may seem cumbersome because it is software and provides many detailed options. A simpler alternative is to use a simple string for your query and use QueryParser to create the query. As a rule, a parser is useful for analyzing user input, it is easier for a programmer to deal with clearly defined fields; for example, if you have the collection you mentioned, it's easy to build it in a for loop.
Collection<String> namesCollection = getNames(); // Contains "billy" and "bob", for example StringBuilder names = new StringBuilder(100); for(String name : namesCollection) { names.append(name).append(" "); // Never mind the space at the end of the resulting string. } QueryBuilder b = fts.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get(); Query luceneQuery = b.bool() .should( // Searches for multiple possible values in the same field b.keyword().onField("firstName").matching( sb.toString() ).createQuery() ) .must(b.keyword().onField("lastName").matching("thornton").createQuery()) .createQuery();
source share