SOLR Spring Data地理空间查询 [英] SOLR Spring Data geospatial query

查看:188
本文介绍了SOLR Spring Data地理空间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用动态查询来搜索SOLR,并且正在尝试为商店定位器功能执行复杂的地理位置查询.它应在距位置(点)的距离D内最多检索5个位置.它还应按距离排序,并在结果中返回距离.

I'm using dynamic queries to search SOLR and I'm trying to execute a complicated geolocation query for a store locator feature. It should retrieve up to 5 locations within a distance D from a location (Point). It should also sort by distance, and return the distance in the result.

我不知道如何获得API输出正确的查询.

I can't figure out how to get the API to output the right query though.

有人可以指导我如何使用Spring SOLR Data API正确获取此查询输出吗?

Could someone please guide me in how to use Spring SOLR Data API to get this query output correctly?

我正在使用SOLR 4+地理过滤器功能.

I'm using SOLR 4+ geofilter feature.

我需要的SOLR查询是这样(直接工作):

The SOLR query I need is this (working directly):

http://localhost:8983/solr/aust/select?q={!func}geodist()&fl=*,score,geodist()&fq={!geofilt}&pt=-37.818635,145.0014704&sfield=location&d=15&fq=docType:LOCATION&rows=5&wt=json&indent=true&sort=score asc

到目前为止,我的代码看起来像这样(不起作用):

And my code so far looks like this (not working):

@Override
    public SimpleSearchResults searchByRegion (SimpleSearchForm searchForm, RegionDocument region)
    {
        logger.entry();


        SimpleQuery query = new SimpleQuery();
        query.addCriteria(new Criteria("text").contains(Criteria.WILDCARD));
        int distance = 5;
        Point point = new org.springframework.data.solr.core.geo.Point(region.getLat(), region.getLng());

        GeoDistanceFunction geo = GeoDistanceFunction.distanceFrom("location").to(point);
        query.addFilterQuery(new SimpleFilterQuery(Criteria.where(geo)));
        SimpleFilterQuery typeQuery = new SimpleFilterQuery();
        typeQuery.addCriteria(new Criteria("docType").is("LOCATION"));

        Sort sort = new Sort(Sort.Direction.ASC, "score");
        query.addFilterQuery(typeQuery);

        // add paging to request
        PageRequest page = new PageRequest(0, 5, sort);
        query.setPageRequest(page);

        // we build the search request without facets initially
        //search.setPageRequest(page);
        Page<LocationDocument> results = solrTemplate1.queryForPage(query, LocationDocument.class);

        logger.exit();

        SimpleSearchResults dto2 = new SimpleSearchResults();
        dto2.setSearchTerm(searchForm.getSearchTerm());
        dto2.setPage(results);

        return dto2;
    }

推荐答案

作为参考,我在发布于以下解决方案的变体上后设法使它起作用:

For reference I managed to get this working after a variation on a solution posted here: How to return distance and score in spatial search using spring-data-solr

因为我使用的是SOLR 4类型的location_rpt,所以geodist()函数不支持该类型.因此,在文档中,他们建议使用score参数返回距离的替代方法. (请注意,它以度数形式返回,需要进行转换=如果有人知道如何在SOLR上进行转换,请在此处发布).

Because I'm using SOLR 4 type of location_rpt, the geodist() function didn't support the type. So in the documentation they suggest an alternative of returning the distance using the score parameter. (note it's returned as degrees and needs to be converted = if anyone knows how to do that conversion on SOLR then please post it here).

因此,我最终使用了一个简单的字符串条件,将score = distance短语添加到了Geofilter表达式中.

So I ended up using a simple String Criteria to add the score=distance phrase into the Geofilter expression.

这是我的代码,用于获取地理过滤器的结果,并使用Spring Data SOLR API和location_rpt模式类型按距离排序.

Here's my code to get the results of a geofilter, sorted by distance using the Spring Data SOLR API and location_rpt schema type.

@Override
    public SimpleSearchResults searchByRegion(SimpleSearchForm searchForm, RegionDocument region) {
        logger.entry();


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

        StringBuffer buf = new StringBuffer("");
        buf.append("{!geofilt pt=");
        buf.append(region.getLat());
        buf.append(",");
        buf.append(region.getLng());
        buf.append(" sfield=location d=");
        buf.append(5.0);
        buf.append(" score=distance}");

        query.addCriteria(new SimpleStringCriteria(buf.toString()));

        FilterQuery docType = new SimpleFilterQuery();
        docType.addCriteria(new Criteria("docType").is("LOCATION"));

        query.addFilterQuery(docType);

        query.addSort(new Sort(Sort.Direction.ASC, "score"));

        Page<LocationDocument> results = solrTemplate1.queryForPage(query, LocationDocument.class);


        SimpleSearchResults dto2 = new SimpleSearchResults();
        dto2.setSearchTerm(searchForm.getSearchTerm());

        if (results.getContent().isEmpty()) {
            logger.debug("results were empty.");
        } else {
            dto2.setPage(results);
        }

        logger.exit();

        return dto2;
    }

这篇关于SOLR Spring Data地理空间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆