How to return distance and score in spatial search using spring-data-solr

前端 未结 1 975
执笔经年
执笔经年 2021-01-01 04:57

I\'m writing a Spring MVC based Search API using spring-data-solr 1.0.0.RELEASE with Solr 4.4.0 and Spring 3.2.4.RELEASE.

I\'m able to run basic queries but unable t

相关标签:
1条回答
  • 2021-01-01 05:40

    To set fl you have to use query.addProjectionOnField(String fieldname).

        SimpleQuery query = new SimpleQuery(conditions);
        query.addProjectionOnField("*");
        query.addProjectionOnField("score");
    

    For mapping score into EventDocument you'll have to add the property as follows.

        @Indexed(value = "score", readonly = true)
        private Float score;
    

    Unfortunately there seems to be an issue with geodist() that might be caused by the way spring data solr creates spatial query. Opended DATASOLR-130 for that.


    Distance can be requested using SolrCallback along with SolrTemplate by setting spatial paramters yourself.

        SimpleQuery query = new SimpleQuery(conditions);
        query.addProjectionOnField("*");
        query.addProjectionOnField("distance:geodist()");
    
        DefaultQueryParser qp = new DefaultQueryParser();
        final SolrQuery solrQuery = qp.constructSolrQuery(query);
        solrQuery.add("sfield", "store");
        solrQuery.add("pt", GeoConverters.GeoLocationToStringConverter.INSTANCE.convert(new GeoLocation(45.15, -93.85)));
        solrQuery.add("d", GeoConverters.DistanceToStringConverter.INSTANCE.convert(new Distance(5)));
    
        List<EventDocument> result = template.execute(new SolrCallback<List<EventDocument>>() {
    
          @Override
          public List<EventDocument> doInSolr(SolrServer solrServer) throws SolrServerException, IOException {
            return template.getConverter().read(solrServer.query(solrQuery).getResults(), EventDocument.class);
          }
        });
    

    A litte bit easier would be to provide required parameters for geodist().

        SimpleQuery query = new SimpleQuery(conditions);
        query.addProjectionOnField("*");
        query.addProjectionOnField("distance:geodist(store," + GeoConverters.GeoLocationToStringConverter.INSTANCE.convert(new GeoLocation(45.15, -93.85)) + ")");
        Page<EventDocument> result = template.queryForPage(query, EventDocument.class);
    

    hope that helps!

    0 讨论(0)
提交回复
热议问题