Search code examples
luceneignite

Ignite TextQueries on multiple fields


I have the following object:

@Data
@AllArgsConstructor
public class SearchItem implements Serializable {
    private String id;
    @QueryTextField
    private String description;
    @QueryTextField
    private String brand;
}

As you can see I have two fileds enabled with the lucene index. Im confused now how to create the TextQuery to search only for a specific field or for both fields. I want to be able to make text searches in the brand field or the descritption field or in both. There is an ignite example showing how to create a TextQuery

private static void textQuery() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);

    //  Query for all people with "Master Degree" in their resumes.
    QueryCursor<Cache.Entry<Long, Person>> masters =
        cache.query(new TextQuery<Long, Person>(Person.class, "Master"));

    // Query for all people with "Bachelor Degree" in their resumes.
    QueryCursor<Cache.Entry<Long, Person>> bachelors =
        cache.query(new TextQuery<Long, Person>(Person.class, "Bachelor"));

    print("Following people have 'Master Degree' in their resumes: ", masters.getAll());
    print("Following people have 'Bachelor Degree' in their resumes: ", bachelors.getAll());
}

But this example is only showing that I have to pass in the class and the search string. How can I define for which field of the class the search should be performed, when I have more then one field annotated with @QueryTextField?

Example search string:

description = ?, brand = ?

Solution

  • You can use query syntax from Apache Lucene: https://lucene.apache.org/core/2_9_4/queryparsersyntax.html

    If it does not work with your field names, I would recommend using names in uppercase instead.