1

我是 Spring 的 Solr 初学者。我正在使用 Solr 4.6 和 Spring 3.x

我已成功将 Solr 配置到我的 Spring 应用程序,并且可以使用以下代码搜索地址。

@Query("text:*?0*")
public List<AddressDocument> findAll(String searchText);

我的架构是

<fields>
    <field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
    <field name="name" type="string" indexed="false" stored="true" required="true" multiValued="false"/>
    <field name="number" type="string" indexed="false" stored="true" required="true" multiValued="false"/>
    <field name="address" type="text_general" indexed="false" stored="true" required="true" multiValued="false"/>
    <field name="city" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="state" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="zipcode" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="country" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="latlng" type="string" indexed="false" stored="true" multiValued="false"/>
    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>
    <field name="_version_" type="long" indexed="true" stored="true"/>
</fields>

<!-- Configure unique key -->
<uniqueKey>id</uniqueKey>
<copyField source="name" dest="text"/>
<copyField source="number" dest="text"/>
<copyField source="address" dest="text"/>
<copyField source="city" dest="text"/>
<copyField source="state" dest="text"/>
<copyField source="zipcode" dest="text"/>
<copyField source="country" dest="text"/>

现在我已经集成了拼写检查器,当我输入任何不正确的拼写时,它会在日志中显示以下响应。

例如,Fargo如果我输入正确的城市名称,Frgo那么它会在输出下方显示我。

2014-01-30 04:09:10,903 DEBUG [http-8080-2] (DirectSolrSpellChecker.java:179) - getSuggestions: [frgo]
2014-01-30 04:09:10,940 DEBUG [http-8080-2] (SpellCheckCollator.java:180) - Collation: text:*fargo* will return 3 hits.
2014-01-30 04:09:10,941  INFO [http-8080-2] (SolrCore.java:1864) - [customer_site_address] webapp=null path=/select params={q=text%3A*frgo*&start=0&rows=0} hits=0 status=0 QTime=43 
2014-01-30 04:09:10,944 DEBUG [http-8080-2] (SolrTemplate.java:326) - Executing query 'q=text%3A*frgo*&start=0&rows=1' against solr.
2014-01-30 04:09:10,951 DEBUG [http-8080-2] (DirectSolrSpellChecker.java:179) - getSuggestions: [frgo]
2014-01-30 04:09:10,959 DEBUG [http-8080-2] (SpellCheckCollator.java:180) - Collation: text:*fargo* will return 3 hits.
2014-01-30 04:09:10,960  INFO [http-8080-2] (SolrCore.java:1864) - [customer_site_address] webapp=null path=/select params={q=text%3A*frgo*&start=0&rows=1} hits=0 status=0 QTime=15 
2014-01-30 04:09:10,961 DEBUG [http-8080-2] (AbstractMessageConverterMethodProcessor.java:150) - Written [[]] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@1fb032d]

正如您在日志中看到的那样,它建议了正确的单词,但我不知道如何使用@Query或任何其他注释来获得这些建议。

我做了很多谷歌,但没有找到任何完美的解决方案。

4

2 回答 2

2

可以使用 收集拼写检查结果SolrTemplage.execute

final String query = "foo"; //any value to be used in solr query

SpellcheckedPage<ExampleSolrBean> page = solrTemplate.execute(new SolrCallback<SpellcheckedPage<ExampleSolrBean>>() {

    @Override
    public SpellcheckedPage<ExampleSolrBean> doInSolr(SolrServer solrServer) throws SolrServerException, IOException {
        SolrQuery q = new SolrQuery("name:"+query);

        ModifiableSolrParams params = new ModifiableSolrParams() {
            {
                add("spellcheck.build", "true");
                add("spellcheck.q", query);
                add("spellcheck", "true");
            }
        };
        q.add(CommonParams.QT, "/spell");
        q.add(params);

        QueryResponse response = solrServer.query(q);

        //init page with search result
        SpellcheckedPage<ExampleSolrBean> page = new SpellcheckedPage<ExampleSolrBean>(solrTemplate.getConverter().read(response.getResults(), ExampleSolrBean.class));

        //add spellcheck result
        SpellCheckResponse scr = response.getSpellCheckResponse();
        for (Suggestion suggestion : scr.getSuggestions()) {
            page.addSuggestions(suggestion.getToken(), suggestion.getAlternatives());
        }

        return page;
    }
});

class SpellcheckedPage<T> extends SolrResultPage<T> {

    public SpellcheckedPage(List<T> content) {
        super(content);
    }

    private Map<String, List<String>> suggestions = new LinkedHashMap<String, List<String>>();

    public void addSuggestions(String term, List<String> suggestions) {
        this.suggestions.put(term, suggestions);
    }

    public Collection<String> getSuggestions(String term) {
        return this.suggestions.get(term);
    }

    public Collection<String> getSuggestions() {
        List<String> allSuggestions = new ArrayList<String>();
        for (List<String> suggestions : this.suggestions.values()) {
            allSuggestions.addAll(suggestions);
        }
        return allSuggestions;
    }
}
于 2014-01-30T12:11:42.630 回答
0

我已经应用了以下逻辑。

ModifiableSolrParams params = new ModifiableSolrParams();
            params.set("qt", "/spell");
            params.set("q", searchText);
            params.set("spellcheck", "on");

            QueryResponse response = solrServer.query(params);
            SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
            if (!spellCheckResponse.isCorrectlySpelled()) {
                for (Suggestion suggestion : response.getSpellCheckResponse().getSuggestions()) {
                    logger.debug("Alternative = "+suggestion.getAlternatives());
                    alternatives.addAll(suggestion.getAlternatives());
                }
            }

这个对吗?

于 2014-01-30T12:42:42.810 回答