1977
1977

Reputation: 2910

Spring Data Solr - @Query parameters "?10"

Hi I have the following query but spring data solr is putting in param 1 value instead of param 10 value for the last param: "AND days_ss:(?10)"... I guess it sees ?1 and not ?10

anyway around this, I tried using :days like one can do with JPA, but this interferes with lucene syntax

    @Highlight(prefix = "<b>", postfix = "</b>")
    @Query("""text:(?0) AND moduleLevel_s:(?1) AND programOfStudy_s:\"?2\" AND yearOfEntry_i:?3 AND yearOfStudy_i:?4 AND unitValue_d:?5 AND
department_s:(?6) AND teachers_ss:(?7) AND cappedAccess_b:?8 AND terms_ss:(?9) AND days_ss:(?10)""")
    HighlightPage<CourseGuide> advancedSearch(@Param(value = "query") List<String> query,
                                     @Param(value = "moduleLevel") List<ModuleLevel> moduleLevel,
                                     @Param(value = "programOfStudy") String programOfStudy,
                                     @Param(value = "yearOfEntry") def yearOfEntry,
                                     @Param(value = "yearOfStudy") def yearOfStudy,
                                     @Param(value = "unitValue") def unitValue,
                                     @Param(value = "department") List<String> department,
                                     @Param(value = "teachers") List<String> teachers,
                                     @Param(value = "cappedAccess") def cappedAccess,
                                     @Param(value = "terms") List<String> terms,
                                     @Param(value = "days") List<String> days,Pageable pageable)

actually whats happening is that during its param replacement process, its looking for all occurrences of '?1' in the string and mistakenly messing with my ?10 placeholder. If it worked in the other direction things would probably be ok, ie process ?10 first then ?9, then ?8 etc

Upvotes: 0

Views: 1011

Answers (1)

Thomas Kerambloch
Thomas Kerambloch

Reputation: 21

I have the same problem. I create a Jira on Spring-data-solr for this problem, https://jira.spring.io/browse/DATASOLR-296?jql=text%20~%20%22%40Query%22%20ORDER%20BY%20created%20DESC

But I find a solution.

You need to use solrTemplate.

Exemple:

In your solrConfiguration.java

@Configuration
@EnableSolrRepositories(value = "com.project.repository.solr",     multicoreSupport = true)
public class SolrConfiguration implements EnvironmentAware{

    private RelaxedPropertyResolver propertyResolver;
    private Environment             environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.data.solr.");
    }

    @Bean
    public SolrServer solrServer() {
        String solrHost = propertyResolver.getProperty("host");
        return new HttpSolrServer(solrHost);
    }

    @Bean(name = "testSolrTemplate")
    public SolrOperations testSolrTemplate() {
        HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
        return new SolrTemplate(httpSolrServer, "TestCore");
    }
} 

Then in your TestRepository.java

public interface TestRepository extends TestRepositoryCustom, SolrRepository<Test, String>, SolrCrudRepository<Test, String> {
}

Then in your TestRepositoryCustom.java

interface TestRepositoryCustom {
    public ArrayList<Test> findTests(
        String arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, String arg8, String arg9, String arg10, String arg11
    );
}

Then in your TestRepositoryImpl.java

public class TestRepositoryImpl implements TestRepositoryCustom {
    @Resource
    private SolrTemplate testSolrTemplate;

    @Override
    public ArrayList<Test> findTests(
        String arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, String arg8, String arg9, String arg10, String arg11
    ) {
        Criteria criteria = new Criteria(Criteria.WILDCARD).expression(Criteria.WILDCARD)
                .and(new Criteria("arg1").expression(arg1))
                .and(new Criteria("arg2").expression(arg2))
                .and(new Criteria("arg3").expression(arg3))
                .and(new Criteria("arg4").expression(arg4))
                .and(new Criteria("arg5").expression(arg5))
                .and(new Criteria("arg6").expression(arg6))
                .and(new Criteria("arg7").expression(arg7))
                .and(new Criteria("arg8").expression(arg8))
                .and(new Criteria("arg9").expression(arg9))
                .and(new Criteria("arg10").expression(arg10))
                .and(new Criteria("arg11").expression(arg11))
        ;

        return new ArrayList<>(testSolrTemplate.queryForPage(new SimpleQuery(criteria).setRows(2147483647), Test.class).getContent());
    }

}

Best, Thomas

Upvotes: 2

Related Questions