checklist
checklist

Reputation: 12930

Autocomplete using Solr & Spring - issue with multiple words

I have indexed a database of locations using Spring Data Solr. I have the following fields:

   <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"/>

I am trying to implement an autocomplete feature. My ajax call is handled by a controller which calls a repository with:

List<POISearch> findByAutocompleteStartingWith(String autocomplete, Pageable pageable);

This works fine with a search like "California" or "Los". But when I try multiple words like "Los Ang" I get an exception:

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)

Any ideas how to solve?

Upvotes: 1

Views: 2846

Answers (2)

checklist
checklist

Reputation: 12930

With the latest version of spring-data-solr you can do the following:

List<POISearch> results = repository.findByAutocompleteStartingWith(Arrays.asList("los", "ange"));

Works great!

Thanks to Christoph for helping with an answer: https://jira.springsource.org/browse/DATASOLR-74

Upvotes: 2

femtoRgon
femtoRgon

Reputation: 33341

Generally, you can't perform a wildcard query on a phrase, which is what it appears is happening here.

I don't know about the implementation of findByAutocompleteStartingWith, but it appears it's just building a PrefixQuery, or something like that. That exception is being thrown by an assertion, so it is designed very explicitly only to make single-word suggestions for single-word queries.

As such, you'll only be able to pass one word into it. Generally, the last word entered.

Upvotes: 0

Related Questions