您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Spring Data Solr在空间搜索中返回距离和得分

如何使用Spring Data Solr在空间搜索中返回距离和得分

要设置fl您必须使用query.addProjectionOnField(String fieldname)

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

为了映射score到其中,EventDocument您必须按如下所示添加属性

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

不幸的是,似乎有一个问题geodist()可能是由Spring数据Solr创建空间查询的方式引起的。为此打开了DATASOLR-130

距离可以使用要求SolrCallback沿着SolrTemplate自己设定的空间paramters。

    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);
      }
    });

稍微容易一点就是为提供所需的参数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);

希望有帮助!

Solr 2022/1/1 18:19:31 有588人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶