rishi
rishi

Reputation: 2554

Hibernate search: spatial+less than condition

I want to add some criteria to hibernate search spatial query. I have a class Store which has latitude and longitude. Also it has a price attribute. I want to find all the stores around a particular location with price point less than given input. This is how my current code looks:

        Session session = getSession(); //getting org.hibernate.Session
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Store.class).get();
        org.apache.lucene.search.Query luceneQuery = builder.spatial().onDefaultCoordinates().within(radius.doubleValue(), Unit.KM).ofLatitude(lat.doubleValue())
                .andLongitude(lon.doubleValue()).createQuery();
        Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery, Store.class);
        @SuppressWarnings("unchecked")
        List<Menu> menus = (List<Menu>) hibQuery.list();

I want to add a criteria to this with less than condition. I can add hibQuery.setParameter("property", "value"); to it. But I am not able to add a criteria like less than. Any pointers on how I can do that?

Upvotes: 2

Views: 367

Answers (1)

RandomMooCow
RandomMooCow

Reputation: 732

You'll want to create a range query on your price field like so:

Query priceQuery = builder.range().onField("price").below(maxPrice)
    .createQuery();

You'll then want to combine your two queries using a boolean query, like so:

Query mainQuery = builder.bool().must(luceneQuery).must(priceQuery)
    .createQuery();

You would use this query to find your results.

Note that to efficiently perform a range query, you'll want to make sure that your price field is annotated with @NumericField along with @Field.

Upvotes: 2

Related Questions