1

Person.java

@Document(indexName = "person", type = "user")
public class Person {

    @Id
    private String id;

    @Field(type = FieldType.String)
    private String name;

    @Field(type = FieldType.Nested)
    private List<Car> cars;

// getter/setter

}

Car.java

public class Car {

    @Field(type = FieldType.String)
    private String name;

    @Field(type = FieldType.String)
    private String model;

// getter/setter
}

Indexing and Searching using main method

public class SpringElasticSearchTest {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
                "src/main/webapp/WEB-INF/spring/applicationContext.xml");

        ElasticsearchTemplate est = (ElasticsearchTemplate) applicationContext
                .getBean("elasticsearchTemplate");

        Person pers = new Person();
        pers.setName("Manish");
        pers.setId("1");

        List<Car> cars = new ArrayList<Car>();
        Car subaru = new Car();
        subaru.setName("Tata");
        subaru.setModel("Safari");
        cars.add(subaru);
        pers.setCars(cars);

        IndexQuery indexQuery = new IndexQuery();
        indexQuery.setId(pers.getId());
        indexQuery.setObject(pers);

        est.deleteIndex("person");

        est.createIndex(Person.class);
        est.putMapping(Person.class);
        est.index(indexQuery);
        est.refresh(Person.class, true);

        QueryBuilder builder = nestedQuery(
                "cars",
                boolQuery().must(termQuery("cars.name", "Tata")).must(
                        termQuery("cars.model", "Safari")));
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                builder).build();
        List<Person> persons = est.queryForList(searchQuery, Person.class);
        System.out.println("Person List : " + persons);

    }

Indexing is working fine but searching functionally is not working.
I am getting the following output:

Person List : []

Please suggest. Thanks in advance.

2 Answers 2

3

Finally I find the solution, The query I have built while searching

termQuery("cars.name", "Tata")

should be in lower case so the correct query would be -

QueryBuilder builder = nestedQuery(
                "cars",
                boolQuery().must(termQuery("cars.name", "tata")).must(
                        termQuery("cars.model", "safari")));
Sign up to request clarification or add additional context in comments.

Comments

0

Another option would have been to use a matchQuery, which is case-insensitive by default:

QueryBuilders.matchQuery("cars.name", "Tata")

Or you can use an analyzer that does not lower case, unlike the standard one.

related discussion here: https://discuss.elastic.co/t/how-to-do-case-insensitive-search-on-terms/19790

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.