gpt4 book ai didi

java - 如何使用 spring-data-solr 在空间搜索中返回距离和分数

转载 作者:搜寻专家 更新时间:2023-11-01 02:10:08 31 4
gpt4 key购买 nike

我正在使用 spring-data-solr 1.0.0.RELEASE 以及 Solr 4.4.0 和 Spring 3.2.4.RELEASE 编写基于 Spring MVC 的搜索 API。

我能够运行基本查询但无法找到任何好的示例如何在结果中返回分数和距离:geodist()。

我知道我可以使用类似

的查询从 Solr 获得结果
http://localhost:8983/solr/events/select?q=*:*&spatial=true&pt=51.435872%2C-0.427529&sfield=position&d=20&facet=true&facet.mincount=1&facet.limit=-1&facet.field=categoryIds&fl=score,*,distance:geodist()&sort=geodist()+asc

但我不知道如何使用 spring-data-solr 获取从 Solr 返回的分数和距离属性。我试过下面的代码

FacetQuery search = new SimpleFacetQuery();

Criteria conditions;// = new Criteria();
if(StringUtils.isNotBlank(searchCriteria.getSearchString()))
conditions = new Criteria(EventDocument.FIELD_EVENT).contains(searchCriteria.getSearchString());

if(searchCriteria.isLocationKnown()){
conditions.and(new Criteria(EventDocument.FIELD_POSITION).within(new GeoLocation(searchCriteria.getLatitude(), searchCriteria.getLongitude()), new Distance(searchCriteria.getDistance(), Distance.Unit.MILES)));
conditions.and(new Criteria(CommonParams.FL).is("score,distance:geodist(),*"));
} else{
conditions.and(new Criteria(CommonParams.FL).is("score,*"));
}

search.addCriteria(conditions);
FacetPage page = solrTemplate.queryForFacetPage(search, EventDocument.class);

但出现以下异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.solr.UncategorizedSolrException: undefined field fl; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: undefined field fl
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

org.springframework.data.solr.UncategorizedSolrException: undefined field fl; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: undefined field fl
org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:122)
org.springframework.data.solr.core.SolrTemplate.executeSolrQuery(SolrTemplate.java:330)
org.springframework.data.solr.core.SolrTemplate.query(SolrTemplate.java:326)
org.springframework.data.solr.core.SolrTemplate.queryForFacetPage(SolrTemplate.java:285)
com.johnstonpress.api.repository.EventDocumentRepositoryImpl.search(EventDocumentRepositoryImpl.java:72)
com.johnstonpress.api.service.SearchEventServiceImpl.search(SearchEventServiceImpl.java:106)
com.johnstonpress.api.controller.EventsSearchController.searchEvents(EventsSearchController.java:124)
com.johnstonpress.api.controller.EventsSearchController.search(EventsSearchController.java:68)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

感谢您的关注和帮助!!!

最佳答案

要设置 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 data solr 创建空间查询的方式引起的。已开通DATASOLR-130为了那个原因。


可以使用 SolrCallbackSolrTemplate 自行设置空间参数来请求距离。

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

希望对您有所帮助!

关于java - 如何使用 spring-data-solr 在空间搜索中返回距离和分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332197/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com