package won.matcher.solr.query;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import won.matcher.solr.config.SolrMatcherConfig;
import won.matcher.solr.hints.HintBuilder;
import won.matcher.solr.query.factory.MatchingContextQueryFactory;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * Created by hfriedrich on 12.08.2016.
 */
@Component
public class DefaultMatcherQueryExecuter implements SolrMatcherQueryExecutor {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    SolrMatcherConfig config;

    SolrClient solrClient;

    @PostConstruct
    private void init() {
        solrClient = new HttpSolrClient.Builder(config.getSolrEndpointUri(false)).build();
    }

    @Override
    public SolrDocumentList executeNeedQuery(String queryString, int maxHints, SolrParams params, String... filterQueries)
            throws IOException, SolrServerException {

        if (queryString == null) {
            log.debug("query string is null, do execute any query!");
            return null;
        }

        SolrQuery query = new SolrQuery();
        log.debug("use query: {} with filters {}", queryString, filterQueries);
        query.setQuery(queryString);
        query.setFields("id", "score", HintBuilder.WON_NODE_SOLR_FIELD, HintBuilder.HAS_FLAG_SOLR_FIELD, MatchingContextQueryFactory.MATCHING_CONTEXT_SOLR_FIELD);
        query.setRows(maxHints);

        if (filterQueries != null) {
            query.setFilterQueries(filterQueries);
        }

        if (params != null) {
            query.add(params);
        }

        try {
            QueryResponse response = solrClient.query(query);
            return response.getResults();
        } catch (SolrException e) {
            log.warn("Exception {} thrown for query: {}", e, queryString);
        }

        return null;
    }

}

@@ -22,24 +22,93 @@
 olr.query;%0A%0A
+import java.io.IOException;%0A%0Aimport javax.annotation.PostConstruct;%0A%0A
 import org.a
@@ -135,32 +135,32 @@
 lrj.SolrClient;%0A
-
 import org.apach
@@ -671,24 +671,25 @@
 .Component;%0A
+%0A
 import won.m
@@ -841,76 +841,8 @@
 y;%0A%0A
-import javax.annotation.PostConstruct;%0Aimport java.io.IOException;%0A%0A
 /**%0A
@@ -2214,36 +2214,753 @@
     
-return response.getResults()
+SolrDocumentList results = response.getResults();%0A            //handle special case: if all results have the same score, the rows parameter does not properly restrict the size%0A            //in order to enforce the restriction, we are doing it here.%0A            if (results.size() %3E maxHints) %7B%0A              SolrDocumentList cappedResults = new SolrDocumentList();%0A              for (int i = 0; i %3C maxHints; i++) %7B%0A                cappedResults.add(results.get(i));%0A              %7D%0A              cappedResults.setMaxScore(results.getMaxScore());%0A              cappedResults.setNumFound(results.getNumFound());%0A              cappedResults.setStart(results.getStart());%0A              return cappedResults;%0A            %7D%0A            return results
 ;%0A  

Enforce solr query result size restriction