Example usage for org.apache.solr.common.params SolrParams getInt

List of usage examples for org.apache.solr.common.params SolrParams getInt

Introduction

In this page you can find the example usage for org.apache.solr.common.params SolrParams getInt.

Prototype

public int getInt(String param, int def) 

Source Link

Document

Returns the int value of the param, or def if not set

Usage

From source file:GeonamesQParserPlugin.java

License:Apache License

public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {
        @Override//from   w  ww  .j  av a  2s .  co  m
        public Query parse() throws ParseException {
            final int rows = localParams.getInt("rows", 1);
            final int start = localParams.getInt("start", 0);
            String topo = localParams.get("topo");
            String distFunc = localParams.get("dist");
            String latFieldName = localParams.get("lat");
            String lonFieldName = localParams.get("lon");
            String ghFieldName = localParams.get("gh");
            String units = localParams.get("unit", "M");
            float boost = localParams.getFloat("boost", 1.0f);
            double radius = Constants.EARTH_RADIUS_MI;
            if (units.equalsIgnoreCase("KM")) {
                radius = Constants.EARTH_RADIUS_KM;
            }
            ValueSource vs = null, latVS = null, lonVS = null, ghVS = null;
            IndexSchema schema = req.getSchema();
            DistType distType = DistType.NORM;
            float power = 2;
            List<ValueSource> latLon = new ArrayList<ValueSource>(2);
            if (ghFieldName != null && ghFieldName.equals("") == false) {
                FieldType ft = schema.getFieldType(ghFieldName);
                SchemaField sf = schema.getField(ghFieldName);
                ghVS = ft.getValueSource(sf, this);//Should we pass this here?
                distType = DistType.GHHSIN;
            } else if (latFieldName != null && latFieldName.equals("") == false && lonFieldName != null
                    && lonFieldName.equals("") == false) {
                FieldType ftLat = schema.getFieldType(latFieldName);
                FieldType ftLon = schema.getFieldType(lonFieldName);
                SchemaField sfLat = schema.getField(latFieldName);
                SchemaField sfLon = schema.getField(lonFieldName);
                latVS = ftLat.getValueSource(sfLat, this);
                lonVS = ftLon.getValueSource(sfLon, this);
                latLon.add(latVS);
                latLon.add(lonVS);
                try {
                    power = Float.parseFloat(distFunc);
                    distType = DistType.NORM;
                } catch (NumberFormatException e) {
                    if (distFunc.equals("hsin")) {
                        distType = DistType.HSIN;
                    } else if (distFunc.equals("ghsin")) {
                        distType = DistType.GHHSIN;
                    }
                }
            } else {
                throw new ParseException("Either gh or both lat and lon must be specified.");
            }

            ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
            searchCriteria.setQ(topo);
            searchCriteria.setMaxRows(rows);
            searchCriteria.setStartRow(start);
            Query query = null;
            try {
                ToponymSearchResult searchResult = WebService.search(searchCriteria);

                List<Toponym> topos = searchResult.getToponyms();
                if (topos.size() > 1) {
                    BooleanQuery tmp = new BooleanQuery();
                    for (Toponym toponym : topos) {
                        double lat = toponym.getLatitude();
                        double lon = toponym.getLongitude();
                        FunctionQuery fq = getFunction(distType, ghVS, power, latLon, lat, lon, radius);
                        tmp.add(fq, BooleanClause.Occur.SHOULD);
                    }
                    query = tmp;
                } else if (topos.size() == 1) {
                    Toponym curr = topos.get(0);
                    query = getFunction(distType, ghVS, power, latLon, curr.getLatitude(), curr.getLongitude(),
                            radius);
                }
            } catch (Exception e) {
                //TODO: deal with errors
            }
            query.setBoost(boost);
            return query;
        }

    };
}

From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {

    SolrParams params = req.getParams();

    if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) {

        String q = params.get(CommonParams.Q);
        if (q == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter"));
            return;
        }//from ww  w.j  a  va  2 s . co m

        String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null
                ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD)
                : FIELDS;
        if (fields == null) {
            rsp.add("error",
                    error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter"));
            return;
        }

        String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF);
        if (df == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter"));
            return;
        }

        int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT);
        if (limit < 1) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0"));
            return;
        }

        String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS;

        Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE);

        //TODO replace
        if (multivalue) {
            rsp.add("error", error(500, "Multivalue suggestions are not yet supported!"));
            return;
        }

        suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue);

    } else {
        super.handleRequestBody(req, rsp);
    }
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

@Override
public NamedList<Object> request(SolrRequest request) throws SolrServerException, IOException {
    SolrParams params = request.getParams();
    if (params == null) {
        params = new ModifiableSolrParams();
    }/*from   ww w.  java2s  .  c o m*/

    String openSearchURL = params.get("openSearchURL");
    if (openSearchURL == null) {
        throw new SolrServerException("openSearchURL param is missing");
    }
    String query = params.get(CommonParams.Q);
    int start = params.getInt(CommonParams.START, 0);
    int hitsPerPage = params.getInt(CommonParams.ROWS, 10);
    String lang = params.get("lang");

    Map<String, String> paramsMap = new HashMap<String, String>();
    if (SimpleSearch.SEARCH_ALL.equals(query)) {
        query = "url:http";
    }
    paramsMap.put("query", query);
    if (StringUtils.isNotBlank(lang)) {
        paramsMap.put("lang", "" + lang);
    }
    paramsMap.put("start", "" + start);
    paramsMap.put("hitsPerPage", "" + hitsPerPage);
    // FIXME
    paramsMap.put("hitsPerDup", "" + Integer.MAX_VALUE);
    Element rootElement = sendGet(openSearchURL, paramsMap);

    SolrDocumentList solrDocumentList = parse(rootElement);
    SimpleOrderedMap<Object> result = new SimpleOrderedMap<Object>();
    result.add("response", solrDocumentList);
    return result;
}

From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    // Date startTime =new Date();

    String event = params.get("event");
    String ids = params.get("ids");
    String shardUrl = params.get("shard.url");

    // filter the query warming clause and di request
    if (!((event != null && event.equals("firstSearcher")) || (ids != null))) {
        String[] shardUrlStrs = shardUrl.split("\\\\|")[0].split("/");
        String collectionName = shardUrlStrs[shardUrlStrs.length - 1];
        String queryText = rb.getQueryString();
        String queryTextAnalyzed = queryText;
        // String queryTextAnalyzed =
        // AnalyzerUtils.analyze(queryText,collection);
        int searchPage = params.getInt("page", 0);
        String simpleSearchStr = getSimpleSearchStr(params);
        String simpleSearchId = getSimpleSearchId(simpleSearchStr);
        String searchLogDocId = generateSearchLogDocId(simpleSearchId);

        String simpleSearchQueryAnalyzedStr = params.toString();
        // String simpleSearchQueryAnalyzedStr
        // =getSimpleSearchStr(simpleSearch, true);
        long timeCost = rb.rsp.getEndTime() - rb.req.getStartTime();

        SolrInputDocument doc = new SolrInputDocument();
        doc.setField(StatsConstants.INDEX_FIELD_ID, searchLogDocId);
        doc.setField(StatsConstants.INDEX_FIELD_COLLECTION_NAME, collectionName);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_ID, simpleSearchId);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH, simpleSearchStr);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_QUERY_ANALYZED, simpleSearchQueryAnalyzedStr);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT, queryText);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT_ANALYZED, queryTextAnalyzed);
        doc.setField(StatsConstants.INDEX_FIELD_NUM_FOUND, rb.getNumberDocumentsFound());
        doc.setField(StatsConstants.INDEX_FIELD_RESPONSE_TIME, timeCost);
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_DATE, new Date());
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_PAGE, searchPage);

        try {//  w  w w  .j av  a  2  s .co  m
            //            searchLogCache.add(doc);
            //            if (searchLogCache.size() >= commitThreshold) {
            int port = Integer.parseInt(shardUrl.substring(shardUrl.indexOf(":") + 1, shardUrl.indexOf("/")));
            if (searchLogServer == null || localPort != port) {
                localPort = port;
                searchLogServer = new HttpSolrServer(
                        "http://localhost:" + localPort + "/solr/" + searchLogCoreName);
            }
            //               searchLogServer.add(searchLogCache);
            searchLogServer.add(doc);
            searchLogServer.commit();
            //               searchLogCache.clear();
            //            }
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // System.out.println("premier phase:"+timeCost);
    }
    // Date endTime =new Date();
    // System.out.println("total time:"+(endTime.getTime()-startTime.getTime()));
}

From source file:com.frank.search.common.PagerComponent.java

License:GNU General Public License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    /* get request params */
    SolrParams par = rb.req.getParams();
    int rows = par.getInt(CommonParams.ROWS, 0);
    int start = par.getInt(CommonParams.START, 0);
    int pages = par.getInt(PARAM_PAGER, 0);
    int pages_pre = par.getInt(PARAM_PAGER_PRE, 2);

    /* neet to work ? */
    if (pages == 0 || rows == 0)
        return;/*from   w  w  w. j  a va  2s .c o m*/

    /* pager list */
    NamedList lst = new SimpleOrderedMap<Object>();
    NamedList lst2 = new SimpleOrderedMap<Object>();

    /* paging pages */
    int doc_count = rb.getResults().docSet.size();
    int page_count = doc_count / rows;
    int page_actual = start / rows;
    int page_pre = pages_pre;
    int page_post = pages - page_pre - 1;

    /* page range */
    if (page_actual - page_pre < 0) {
        page_post += -(page_actual - page_pre);
        page_pre -= -(page_actual - page_pre);
    } else if (page_actual + page_post > page_count) {
        page_post = pages - page_pre;
        page_pre = page_actual + pages - page_count;
    }

    /* sanity */
    if (page_pre < 0)
        page_pre = 0;
    if (page_post < 0)
        page_post = 0;

    /* next pages list */
    int i = (page_actual - page_pre);
    for (i = (i <= 0 ? 0 : i); i <= page_count && i <= (page_actual + page_post); i++)
        lst2.add(Integer.toString(i + 1), i * rows);
    lst.add("pages", lst2);

    /* navi */
    if (page_actual > 0)
        lst.add("prev", (page_actual - 1) * rows);
    if (page_actual - page_pre > 0)
        lst.add("first", 0);
    if (page_actual < page_count)
        lst.add("next", (page_actual + 1) * rows);
    if (page_actual + page_post < page_count)
        lst.add("last", page_count * rows);
    lst.add("actual", page_actual + 1);
    lst.add("count", page_count);

    /* finish */
    rb.rsp.add("pager", lst);
}

From source file:com.grantingersoll.intell.clustering.KMeansClusteringEngine.java

License:Apache License

@Override
public NamedList cluster(SolrParams params) {
    NamedList result = new NamedList();
    //check to see if we have new results
    try {// w  w  w .j  a  v  a 2s .  c  o m
        if (theFuture != null) {
            //see if we have new results, but don't wait too long for them
            ClusterJob job = theFuture.get(1, TimeUnit.MILLISECONDS);
            if (lastSuccessful != null) {
                //clean up the old ones
                //TODO: clean up the old dirs before switching lastSuccessful
            }
            lastSuccessful = job;
            theFuture = null;
        } else {

        }

    } catch (InterruptedException e) {
        log.error("Exception", e);
    } catch (ExecutionException e) {
        log.error("Exception", e);
    } catch (TimeoutException e) {
        log.error("Exception", e);
    }
    if (lastSuccessful != null) {//we have clusters
        //do we need the points?
        boolean includePoints = params.getBool(INCLUDE_POINTS, false);
        int clusterId = params.getInt(LIST_POINTS, Integer.MIN_VALUE);
        Map<Integer, List<String>> toPoints = lastSuccessful.clusterIdToPoints;
        String docId = params.get(IN_CLUSTER);
        if ((includePoints || clusterId != Integer.MIN_VALUE || docId != null) && toPoints == null) {
            //load the points
            try {
                toPoints = readPoints(new Path(lastSuccessful.jobDir + File.separator + "points"),
                        lastSuccessful.conf);
            } catch (IOException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                        "Unable to load points: " + lastSuccessful);
            }
        }
        if (params.getBool(LIST_CLUSTERS)) {
            NamedList nl = new NamedList();
            result.add("all", nl);

            Map<Integer, Cluster> clusterMap = lastSuccessful.clusters;
            if (clusterMap == null) {
                //we aren't caching, so load 'em up
                try {
                    clusterMap = loadClusters(lastSuccessful);
                } catch (Exception e) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                            "unable to load the clusters from " + lastSuccessful);
                }
            }

            for (Cluster cluster : clusterMap.values()) {
                NamedList clusterNL = new NamedList();
                nl.add(String.valueOf(cluster.getId()), clusterNL);
                clusterNL.add("numPoints", cluster.getNumPoints());
                //TODO: better format?
                clusterNL.add("center", cluster.getCenter().asFormatString());
                if (cluster.getRadius() != null) {
                    clusterNL.add("radius", cluster.getRadius().asFormatString());
                }
                if (includePoints) {
                    List<String> points = toPoints.get(cluster.getId());
                    clusterNL.add("points", points);
                }
            }
        }

        if (docId != null) {

        }
        //TODO: support sending in multiple ids

        if (clusterId != Integer.MIN_VALUE) {
            List<String> points = lastSuccessful.clusterIdToPoints.get(clusterId);
            if (points != null) {
                result.add(String.valueOf(clusterId), points);
            }
        }
    } else if (params.getBool(BUILD, false)) {
        RefCounted<SolrIndexSearcher> refCnt = core.getSearcher();
        int theK = params.getInt(K, 10);
        cluster(refCnt.get(), theK);
        refCnt.decref();
    }
    return result;
}

From source file:com.grantingersoll.intell.clustering.KMeansClusteringEngine.java

License:Apache License

@Override
public String init(NamedList config, SolrCore core) {
    String result = super.init(config, core);
    SolrParams params = SolrParams.toSolrParams(config);
    this.core = core;
    String dirStr = params.get("dir");
    clusterBaseDir = new File(dirStr);
    if (clusterBaseDir.isAbsolute() == false) {
        clusterBaseDir = new File(core.getDataDir(), dirStr);
    }/* w  ww  .jav a  2 s  .c  o m*/
    clusterBaseDir.mkdirs();
    inputField = params.get("inputField");
    String distMeas = params.get("distanceMeasure");
    Class distClass = core.getResourceLoader().findClass(distMeas);

    try {
        measure = (DistanceMeasure) distClass.newInstance();
    } catch (InstantiationException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to load measure class", e);
    } catch (IllegalAccessException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to load measure class", e);
    }
    convergence = params.getDouble("convergence", 0.001);
    maxIters = params.getInt("maxIterations", 20);
    cacheClusters = params.getBool("cacheClusters", true);
    cachePoints = params.getBool("cachePoints", true);
    this.k = params.getInt("k");
    //See if we have clusters already
    File nowFile = new File(clusterBaseDir, "lastJob");
    if (nowFile.exists()) {
        lastSuccessful = readJobDetails(nowFile);

    }
    return result;
}

From source file:com.o19s.solr.swan.highlight.SwanHighlighter.java

License:Apache License

/**
 * Generates a list of Highlighted query fragments for each item in a list
 * of documents, or returns null if highlighting is disabled.
 *
 * @param docs query results/*  ww  w  . j  a  v  a  2s .c  o m*/
 * @param query the query
 * @param req the current request
 * @param defaultFields default list of fields to summarize
 *
 * @return NamedList containing a NamedList for each document, which in
 * turns contains sets (field, summary) pairs.
 */
@Override
@SuppressWarnings("unchecked")
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields)
        throws IOException {

    NamedList fragments = new SimpleOrderedMap();

    SolrParams params = req.getParams();
    if (!isHighlightingEnabled(params))
        return null;

    SolrIndexSearcher searcher = req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    String[] fieldNames = getHighlightFields(query, req, defaultFields);
    Set<String> fset = new HashSet<String>();

    {
        // pre-fetch documents using the Searcher's doc cache
        Collections.addAll(fset, fieldNames);
        // fetch unique key if one exists.
        SchemaField keyField = schema.getUniqueKeyField();
        if (null != keyField)
            fset.add(keyField.getName());
    }

    //CHANGE start
    //       int[] docIds = new int[docs.swordize()];
    TreeSet<Integer> docIds = new TreeSet<Integer>();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        docIds.add(iterator.nextDoc());
    }
    // Get Frag list builder
    String fragListBuilderString = params.get(HighlightParams.FRAG_LIST_BUILDER).toLowerCase();
    FragListBuilder fragListBuilder;
    if (fragListBuilderString.equals("single")) {
        fragListBuilder = new SingleFragListBuilder();
    } else {
        fragListBuilder = new com.o19s.solr.swan.highlight.SimpleFragListBuilder();
    }

    // get FastVectorHighlighter instance out of the processing loop
    SpanAwareFastVectorHighlighter safvh = new SpanAwareFastVectorHighlighter(
            // FVH cannot process hl.usePhraseHighlighter parameter per-field basis
            params.getBool(HighlightParams.USE_PHRASE_HIGHLIGHTER, true),
            // FVH cannot process hl.requireFieldMatch parameter per-field basis
            params.getBool(HighlightParams.FIELD_MATCH, false), fragListBuilder,
            //new com.o19s.solr.swan.highlight.ScoreOrderFragmentsBuilder(),
            new WordHashFragmentsBuilder(),
            // List of docIds to filter spans
            docIds);
    safvh.setPhraseLimit(params.getInt(HighlightParams.PHRASE_LIMIT, Integer.MAX_VALUE));
    SpanAwareFieldQuery fieldQuery = safvh.getFieldQuery(query, searcher.getIndexReader(), docIds);

    // Highlight each document
    for (int docId : docIds) {
        Document doc = searcher.doc(docId, fset);
        NamedList docSummaries = new SimpleOrderedMap();
        for (String fieldName : fieldNames) {
            fieldName = fieldName.trim();
            if (useFastVectorHighlighter(params, schema, fieldName))
                doHighlightingByFastVectorHighlighter(safvh, fieldQuery, req, docSummaries, docId, doc,
                        fieldName);
            else
                doHighlightingByHighlighter(query, req, docSummaries, docId, doc, fieldName);
        }
        String printId = schema.printableUniqueKey(doc);
        fragments.add(printId == null ? null : printId, docSummaries);
    }
    //CHANGE end
    return fragments;
}

From source file:com.searchbox.solr.CategoryLikeThis.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    numRequests++;/*  ww  w .ja v  a  2s. c om*/
    long startTime = System.currentTimeMillis();
    if (!keystate) {
        LOGGER.error(
                "License key failure, not performing clt query. Please email contact@searchbox.com for more information.");
        return;
    }

    try {
        SolrParams params = req.getParams();
        String senseField = params.get(SenseParams.SENSE_FIELD, SenseParams.DEFAULT_SENSE_FIELD);
        BooleanQuery catfilter = new BooleanQuery();
        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }

        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        SortSpec sortSpec = null;
        List<Query> filters = new LinkedList<Query>();
        List<RealTermFreqVector> prototypetfs = new LinkedList<RealTermFreqVector>();

        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSort(true);
            }

            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, null, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (Exception e) {
            numErrors++;
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }

        SolrIndexSearcher searcher = req.getSearcher();
        DocListAndSet cltDocs = null;

        // Parse Required Params
        // This will either have a single Reader or valid query
        Reader reader = null;
        try {
            if (q == null || q.trim().length() < 1) {
                Iterable<ContentStream> streams = req.getContentStreams();
                if (streams != null) {
                    Iterator<ContentStream> iter = streams.iterator();
                    if (iter.hasNext()) {
                        reader = iter.next().getReader();
                    }
                    if (iter.hasNext()) {
                        numErrors++;
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "SenseLikeThis does not support multiple ContentStreams");
                    }
                }
            }

            int start = params.getInt(CommonParams.START, 0);
            int rows = params.getInt(CommonParams.ROWS, 10);

            // Find documents SenseLikeThis - either with a reader or a query
            // --------------------------------------------------------------------------------
            if (reader != null) {
                numErrors++;
                throw new RuntimeException("SLT based on a reader is not yet implemented");
            } else if (q != null) {

                LOGGER.debug("Query for category:\t" + query);
                DocList match = searcher.getDocList(query, null, null, 0, 10, flags); // get first 10
                if (match.size() == 0) { // no docs to make prototype!
                    LOGGER.info("No documents found for prototype!");
                    rsp.add("response", new DocListAndSet());
                    return;
                }

                HashMap<String, Float> overallFreqMap = new HashMap<String, Float>();
                // Create the TF of blah blah blah
                DocIterator iterator = match.iterator();
                while (iterator.hasNext()) {
                    // do a MoreLikeThis query for each document in results
                    int id = iterator.nextDoc();
                    LOGGER.trace("Working on doc:\t" + id);
                    RealTermFreqVector rtv = new RealTermFreqVector(id, searcher.getIndexReader(), senseField);
                    for (int zz = 0; zz < rtv.getSize(); zz++) {
                        Float prev = overallFreqMap.get(rtv.getTerms()[zz]);
                        if (prev == null) {
                            prev = 0f;
                        }
                        overallFreqMap.put(rtv.getTerms()[zz], rtv.getFreqs()[zz] + prev);
                    }
                    prototypetfs.add(rtv);
                }

                List<String> sortedKeys = Ordering.natural().onResultOf(Functions.forMap(overallFreqMap))
                        .immutableSortedCopy(overallFreqMap.keySet());
                int keyiter = Math.min(sortedKeys.size() - 1, BooleanQuery.getMaxClauseCount() - 1);
                LOGGER.debug("I have this many terms:\t" + sortedKeys.size());
                LOGGER.debug("And i'm going to use this many:\t" + keyiter);
                for (; keyiter >= 0; keyiter--) {
                    TermQuery tq = new TermQuery(new Term(senseField, sortedKeys.get(keyiter)));
                    catfilter.add(tq, BooleanClause.Occur.SHOULD);
                }

            } else {
                numErrors++;
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "CategoryLikeThis requires either a query (?q=) or text to find similar documents.");
            }

            LOGGER.debug("document filter is: \t" + catfilter);
            CategorizationBase model = new CategorizationBase(prototypetfs);
            CategoryQuery clt = CategoryQuery.CategoryQueryForDocument(catfilter, model,
                    searcher.getIndexReader(), senseField);
            DocSet filtered = searcher.getDocSet(filters);
            cltDocs = searcher.getDocListAndSet(clt, filtered, Sort.RELEVANCE, start, rows, flags);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        if (cltDocs == null) {
            numEmpty++;
            cltDocs = new DocListAndSet(); // avoid NPE
        }
        rsp.add("response", cltDocs.docList);

        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (cltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, cltDocs.docSet, params);
                rsp.add("facet_counts", f.getFacetCounts());
            }
        }
    } catch (Exception e) {
        numErrors++;
    } finally {
        totalTime += System.currentTimeMillis() - startTime;
    }

}

From source file:com.searchbox.solr.SenseLikeThisHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    NamedList<Object> timinginfo = new NamedList<Object>();
    numRequests++;//from  ww  w  .j  a  v  a  2  s .  c  o  m
    long startTime = System.currentTimeMillis();
    long lstartTime = System.currentTimeMillis();
    if (!keystate) {
        LOGGER.error(
                "License key failure, not performing sense query. Please email contact@searchbox.com for more information.");
        return;
    }

    boolean fromcache = false;

    try {
        SolrParams params = req.getParams();
        int start = params.getInt(CommonParams.START, 0);
        int rows = params.getInt(CommonParams.ROWS, 10);

        HashSet<String> toIgnore = (new HashSet<String>());
        toIgnore.add("start");
        toIgnore.add("rows");
        toIgnore.add("fl");
        toIgnore.add("wt");
        toIgnore.add("indent");

        SolrCacheKey key = new SolrCacheKey(params, toIgnore);

        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }

        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        QueryReductionFilter qr = null;
        SortSpec sortSpec = null;
        List<Query> filters = new ArrayList<Query>();

        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSort(true);
            }

            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, null, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (Exception e) {
            numErrors++;
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }

        timinginfo.add("Parse Query time", System.currentTimeMillis() - lstartTime);
        LOGGER.debug("Parsed Query Time:\t" + (System.currentTimeMillis() - lstartTime));
        lstartTime = System.currentTimeMillis();

        SolrIndexSearcher searcher = req.getSearcher();
        SchemaField uniqueKeyField = searcher.getSchema().getUniqueKeyField();

        // Parse Required Params
        // This will either have a single Reader or valid query

        // Find documents SenseLikeThis - either with a reader or a query
        // --------------------------------------------------------------------------------
        SenseQuery slt = null;
        if (q == null) {
            numErrors++;
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "SenseLikeThis requires either a query (?q=) or text to find similar documents.");

        }
        // Matching options
        boolean includeMatch = params.getBool(MoreLikeThisParams.MATCH_INCLUDE, true);
        int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
        // Find the base match

        DocList match = searcher.getDocList(query, null, null, matchOffset, 1, flags); // only get the first one...
        if (includeMatch) {
            rsp.add("match", match);
        }

        DocIterator iterator = match.iterator();
        if (!iterator.hasNext()) {
            numErrors++;
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "SenseLikeThis no document found matching request.");
        }
        int id = iterator.nextDoc();

        timinginfo.add("Find Query Doc", System.currentTimeMillis() - lstartTime);
        LOGGER.debug("Find Query Doc:\t" + (System.currentTimeMillis() - lstartTime));
        lstartTime = System.currentTimeMillis();

        SolrCache sc = searcher.getCache("com.searchbox.sltcache");
        DocListAndSet sltDocs = null;
        if (sc != null) {
            //try to get from cache
            sltDocs = (DocListAndSet) sc.get(key.getSet());
        } else {
            LOGGER.error("com.searchbox.sltcache not defined, can't cache slt queries");
        }

        sltDocs = (DocListAndSet) sc.get(key.getSet());
        if (start + rows > 1000 || sltDocs == null || !params.getBool(CommonParams.CACHE, true)) { //not in cache, need to do search
            BooleanQuery bq = new BooleanQuery();
            Document doc = searcher.getIndexReader().document(id);
            bq.add(new TermQuery(new Term(uniqueKeyField.getName(),
                    uniqueKeyField.getType().storedToIndexed(doc.getField(uniqueKeyField.getName())))),
                    BooleanClause.Occur.MUST_NOT);
            filters.add(bq);

            String[] senseFields = splitList
                    .split(params.get(SenseParams.SENSE_FIELD, SenseParams.DEFAULT_SENSE_FIELD));
            String senseField = (senseFields[0] != null) ? senseFields[0] : SenseParams.DEFAULT_SENSE_FIELD;

            //TODO more intelligent handling of multiple fields , can probably do a boolean junction of multiple sensequeries, but this will be slow
            long maxlength = -1;
            for (String possibleField : senseFields) {
                try {
                    long flength = doc.getField(possibleField).stringValue().length();
                    if (flength > maxlength) {
                        senseField = possibleField;
                        maxlength = flength;
                    }
                } catch (Exception e) {
                    System.out.println("Error: " + e.getMessage());
                }
            }

            LOGGER.debug("Using sense field :\t" + (senseField));

            String CKBid = params.get(SenseParams.SENSE_CKB, SenseParams.SENSE_CKB_DEFAULT);

            RealTermFreqVector rtv = new RealTermFreqVector(id, searcher.getIndexReader(), senseField);
            timinginfo.add("Make real term freq vector", System.currentTimeMillis() - lstartTime);
            lstartTime = System.currentTimeMillis();

            qr = new QueryReductionFilter(rtv, CKBid, searcher, senseField);
            qr.setNumtermstouse(params.getInt(SenseParams.SENSE_QR_NTU, SenseParams.SENSE_QR_NTU_DEFAULT));
            qr.setThreshold(params.getInt(SenseParams.SENSE_QR_THRESH, SenseParams.SENSE_QR_THRESH_DEFAULT));
            qr.setMaxDocSubSet(params.getInt(SenseParams.SENSE_QR_MAXDOC, SenseParams.SENSE_QR_MAXDOC_DEFAULT));
            qr.setMinDocSetSizeForFilter(
                    params.getInt(SenseParams.SENSE_MINDOC4QR, SenseParams.SENSE_MINDOC4QR_DEFAULT));

            numTermsUsed += qr.getNumtermstouse();
            numTermsConsidered += rtv.getSize();

            timinginfo.add("Setup SLT query", System.currentTimeMillis() - lstartTime);
            LOGGER.debug("Setup SLT query:\t" + (System.currentTimeMillis() - lstartTime));
            lstartTime = System.currentTimeMillis();

            DocList subFiltered = qr.getSubSetToSearchIn(filters);
            timinginfo.add("Do Query Redux", System.currentTimeMillis() - lstartTime);
            LOGGER.debug("Do query redux:\t" + (System.currentTimeMillis() - lstartTime));
            lstartTime = System.currentTimeMillis();

            numFiltered += qr.getFiltered().docList.size();
            numSubset += subFiltered.size();
            LOGGER.info("Number of documents to search:\t" + subFiltered.size());

            slt = new SenseQuery(rtv, senseField, CKBid,
                    params.getFloat(SenseParams.SENSE_WEIGHT, SenseParams.DEFAULT_SENSE_WEIGHT), null);
            LOGGER.debug("Setup sense query:\t" + (System.currentTimeMillis() - lstartTime));
            timinginfo.add("Setup sense query", System.currentTimeMillis() - lstartTime);
            lstartTime = System.currentTimeMillis();

            sltDocs = searcher.getDocListAndSet(slt, subFiltered, Sort.RELEVANCE, 0, 1000, flags);
            timinginfo.add("Do sense query", System.currentTimeMillis() - lstartTime);
            lstartTime = System.currentTimeMillis();

            LOGGER.debug("Adding this keyto cache:\t" + key.getSet().toString());
            searcher.getCache("com.searchbox.sltcache").put(key.getSet(), sltDocs);

        } else {
            fromcache = true;
            timinginfo.add("Getting from cache", System.currentTimeMillis() - lstartTime);
            LOGGER.debug("Got result from cache");
            lstartTime = System.currentTimeMillis();
        }

        if (sltDocs == null) {
            numEmpty++;
            sltDocs = new DocListAndSet(); // avoid NPE
        }
        rsp.add("response", sltDocs.docList.subset(start, rows));

        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (sltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, sltDocs.docSet, params);
                rsp.add("facet_counts", f.getFacetCounts());
            }
        }
        timinginfo.add("Facet parts", System.currentTimeMillis() - lstartTime);
        LOGGER.debug("Facet parts:\t" + (System.currentTimeMillis() - lstartTime));

        // Debug info, not doing it for the moment.
        boolean dbg = req.getParams().getBool(CommonParams.DEBUG_QUERY, false);

        boolean dbgQuery = false, dbgResults = false;
        if (dbg == false) {//if it's true, we are doing everything anyway.
            String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG);
            if (dbgParams != null) {
                for (int i = 0; i < dbgParams.length; i++) {
                    if (dbgParams[i].equals(CommonParams.QUERY)) {
                        dbgQuery = true;
                    } else if (dbgParams[i].equals(CommonParams.RESULTS)) {
                        dbgResults = true;
                    }
                }
            }
        } else {
            dbgQuery = true;
            dbgResults = true;
        }
        // Copied from StandardRequestHandler... perhaps it should be added to doStandardDebug?
        if (dbg == true) {
            try {
                lstartTime = System.currentTimeMillis();
                NamedList<Object> dbgInfo = SolrPluginUtils.doStandardDebug(req, q, slt,
                        sltDocs.docList.subset(start, rows), dbgQuery, dbgResults);
                dbgInfo.add("Query freqs", slt.getAllTermsasString());
                if (null != dbgInfo) {
                    if (null != filters) {
                        dbgInfo.add("filter_queries", req.getParams().getParams(CommonParams.FQ));
                        List<String> fqs = new ArrayList<String>(filters.size());
                        for (Query fq : filters) {
                            fqs.add(QueryParsing.toString(fq, req.getSchema()));
                        }
                        dbgInfo.add("parsed_filter_queries", fqs);
                    }
                    if (null != qr) {
                        dbgInfo.add("QueryReduction", qr.getDbgInfo());
                    }
                    if (null != slt) {
                        dbgInfo.add("SLT", slt.getDbgInfo());

                    }

                    dbgInfo.add("fromcache", fromcache);
                    rsp.add("debug", dbgInfo);
                    timinginfo.add("Debugging parts", System.currentTimeMillis() - lstartTime);
                    dbgInfo.add("timings", timinginfo);
                }
            } catch (Exception e) {
                SolrException.log(SolrCore.log, "Exception during debug", e);
                rsp.add("exception_during_debug", SolrException.toStr(e));
            }
        }
    } catch (Exception e) {
        numErrors++;
        e.printStackTrace();
    } finally {
        totalTime += System.currentTimeMillis() - startTime;
    }

}