Spring Data Solr地理查询

时间:2013-10-22 15:05:58

标签: solr spring-data-solr

我刚开始在Solr周围玩一下,并设法让它在Tomcat servlet容器中运行。我现在想要使用Spring Data中的存储库方法,但在尝试处理lat / lon字段时(即:地理空间数据)却被卡住了。我想存储一些类似推文的数据。这是我目前正在使用的架构(尝试遵循wiki):

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="tweets" version="1.1">

  <types>

    <fieldType name="string" class="solr.StrField"/>

    <fieldType name="text1" class="solr.TextField">
      <analyzer>
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.HunspellStemFilterFactory" 
                dictionary="../../dictionaries/es_ANY.dic" 
                affix="../../dictionaries/es_ANY.aff" 
                ignoreCase="true" />
        <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
      </analyzer>
    </fieldType>

    <fieldType name="text2" class="solr.TextField">
      <analyzer>
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

    <fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>

    <dynamicField name="*_coordinate"  type="tdouble" indexed="true"  stored="false"/>

    <fieldType name="date" class="solr.DateField"/>

    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>

  </types>

  <fields>
    <field name="id" type="string" indexed="true" stored="true" multiValued="false"/>
    <field name="username" type="string" indexed="true" stored="true" multiValued="false"/>
    <field name="pictureURL" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="topic" type="string" indexed="true" stored="true" multiValued="false"/>
    <field name="content" type="text1" indexed="true" stored="true"/>
    <field name="hashtags" type="text2" indexed="true" stored="true"/>
    <field name="geo" type="location" indexed="true" stored="true"/>
    <field name="timestamp" type="date" indexed="true" stored="true"/>
    <field name="_version_" type="long" indexed="true" stored="true"/>
  </fields>

  <uniqueKey>id</uniqueKey>
  <defaultSearchField>id</defaultSearchField>

</schema>

没有geo字段就可以正常工作,我不知道如何在我的POJO中进行映射(我尝试使用像地理字段中的MongoDB和String这样的double []而没有太大成功):

public class Tweet {

    @Id
    @Field
    private String id;

    @Field
    private String username;

    @Field
    private String pictureURL;

    @Field
    private String topic;

    @Field
    private String content;

    @Field
    private List<String> hashtags;

    @Field
    private String geo;

    @Field
    private Date timestamp;

    /** Getters/setters omitted **/
}

将地理字段映射为简单字符串([lat],[lng])时抛出的异常是:

org.springframework.data.solr.UncategorizedSolrException: undefined field: "geo_0_coordinate"; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: undefined field: "geo_0_coordinate"

我试着查看project tests,但没有找到任何使用地理字段的POJO。

关于如何进行的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。首先,地理字段应该是GeoLocation:

@Field
private GeoLocation geo;

需要进行另一项更改,发生在schema.xml文件中:

<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>
<fieldType name="double" class="solr.DoubleField"/>
<dynamicField name="*_coordinate"  type="tdouble" indexed="true"  stored="false"/>

<!-- ... -->

<field name="geo" type="location" indexed="true" stored="true"/>
<field name="geo_0_coordinate" type="double" indexed="true" stored="true" />
<field name="geo_1_coordinate" type="double" indexed="true" stored="true" />

事实证明,Solr将LatLonTypes内部存储为一对双精度数据,这些双精度数也应在模式中定义。

希望这有助于其他人!