我已经使用 Spring Data Solr 为位置数据库建立了索引。我有以下字段:
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="text_ws" indexed="true" stored="true"/>
<field name="autocomplete" type="lowercase" indexed="true" stored="false"/>
我正在尝试实现自动完成功能。我的 ajax 调用由 Controller 处理,该 Controller 通过以下方式调用存储库:
List<POISearch> findByAutocompleteStartingWith(String autocomplete, Pageable pageable);
这对于“加利福尼亚”或“洛斯”等搜索效果很好。但是当我尝试使用“Los Ang”等多个单词时,我遇到了异常:
SEVERE: Servlet.service() for servlet [spring-mvc] in context with path [/xxx] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Cannot constructQuery '"los an"'. Use epxression or mulitple clauses instead.] with root cause org.springframework.dao.InvalidDataAccessApiUsageException: Cannot constructQuery '"los an"'. Use epxression or mulitple clauses instead. at org.springframework.data.solr.core.query.Criteria.assertNoBlankInWildcardedQuery(Criteria.java:596) at org.springframework.data.solr.core.query.Criteria.contains(Criteria.java:230) at org.springframework.data.solr.core.query.Criteria.contains(Criteria.java:257) at org.springframework.data.solr.core.query.Criteria.contains(Criteria.java:244) at org.springframework.data.solr.repository.query.SolrQueryCreator.from(SolrQueryCreator.java:112) at org.springframework.data.solr.repository.query.SolrQueryCreator.create(SolrQueryCreator.java:56) at org.springframework.data.solr.repository.query.SolrQueryCreator.create(SolrQueryCreator.java:43) at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) at org.springframework.data.solr.repository.query.PartTreeSolrQuery.createQuery(PartTreeSolrQuery.java:46) at org.springframework.data.solr.repository.query.AbstractSolrQuery.execute(AbstractSolrQuery.java:95) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:312) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy44.findByAutocompleteContaining(Unknown Source)
有什么解决办法吗?
最佳答案
使用最新版本的 spring-data-solr,您可以执行以下操作:
List<POISearch> results = repository.findByAutocompleteStartingWith(Arrays.asList("los", "ange"));
效果很好!
感谢 Christoph 帮助解答:https://jira.springsource.org/browse/DATASOLR-74
关于java - 使用 Solr 和 Spring 自动完成 - 多个单词的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441538/