Example usage for org.apache.solr.request SolrQueryRequest getParamString

List of usage examples for org.apache.solr.request SolrQueryRequest getParamString

Introduction

In this page you can find the example usage for org.apache.solr.request SolrQueryRequest getParamString.

Prototype

public String getParamString();

Source Link

Document

Returns a string representing all the important parameters.

Usage

From source file:opennlp.tools.similarity.apps.solr.IterativeSearchRequestHandler.java

License:Apache License

public DocList filterResultsBySyntMatchReduceDocSet(DocList docList, SolrQueryRequest req, SolrParams params) {
    //if (!docList.hasScores()) 
    //   return docList;

    int len = docList.size();
    if (len < 1) // do nothing
        return docList;
    ParserChunker2MatcherProcessor pos = ParserChunker2MatcherProcessor.getInstance();

    DocIterator iter = docList.iterator();
    float[] syntMatchScoreArr = new float[len];
    String requestExpression = req.getParamString();
    String[] exprParts = requestExpression.split("&");
    for (String part : exprParts) {
        if (part.startsWith("q="))
            requestExpression = part;/* ww w.j  a v  a 2 s .c o m*/
    }
    String fieldNameQuery = StringUtils.substringBetween(requestExpression, "=", ":");
    // extract phrase query (in double-quotes)
    String[] queryParts = requestExpression.split("\"");
    if (queryParts.length >= 2 && queryParts[1].length() > 5)
        requestExpression = queryParts[1].replace('+', ' ');
    else if (requestExpression.indexOf(":") > -1) {// still field-based expression
        requestExpression = requestExpression.replaceAll(fieldNameQuery + ":", "").replace('+', ' ')
                .replaceAll("  ", " ").replace("q=", "");
    }

    if (fieldNameQuery == null)
        return docList;
    if (requestExpression == null || requestExpression.length() < 5 || requestExpression.split(" ").length < 3)
        return docList;
    int[] docIDsHits = new int[len];

    IndexReader indexReader = req.getSearcher().getIndexReader();
    List<Integer> bestMatchesDocIds = new ArrayList<Integer>();
    List<Float> bestMatchesScore = new ArrayList<Float>();
    List<Pair<Integer, Float>> docIdsScores = new ArrayList<Pair<Integer, Float>>();
    try {
        for (int i = 0; i < docList.size(); ++i) {
            int docId = iter.nextDoc();
            docIDsHits[i] = docId;
            Document doc = indexReader.document(docId);

            // get text for event
            String answerText = doc.get(fieldNameQuery);
            if (answerText == null)
                continue;
            SentencePairMatchResult matchResult = pos.assessRelevance(requestExpression, answerText);
            float syntMatchScore = new Double(
                    parseTreeChunkListScorer.getParseTreeChunkListScore(matchResult.getMatchResult()))
                            .floatValue();
            bestMatchesDocIds.add(docId);
            bestMatchesScore.add(syntMatchScore);
            syntMatchScoreArr[i] = (float) syntMatchScore; //*iter.score();
            System.out.println(" Matched query = '" + requestExpression + "' with answer = '" + answerText
                    + "' | doc_id = '" + docId);
            System.out.println(" Match result = '" + matchResult.getMatchResult() + "' with score = '"
                    + syntMatchScore + "';");
            docIdsScores.add(new Pair(docId, syntMatchScore));
        }

    } catch (CorruptIndexException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        //log.severe("Corrupt index"+e1);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        //log.severe("File read IO / index"+e1);
    }

    Collections.sort(docIdsScores, new PairComparable());
    for (int i = 0; i < docIdsScores.size(); i++) {
        bestMatchesDocIds.set(i, docIdsScores.get(i).getFirst());
        bestMatchesScore.set(i, docIdsScores.get(i).getSecond());
    }
    System.out.println(bestMatchesScore);
    float maxScore = docList.maxScore(); // do not change
    int limit = docIdsScores.size();
    int start = 0;
    DocSlice ds = null;

    ds = new DocSlice(start, limit, ArrayUtils.toPrimitive(bestMatchesDocIds.toArray(new Integer[0])),
            ArrayUtils.toPrimitive(bestMatchesScore.toArray(new Float[0])), bestMatchesDocIds.size(), maxScore);

    return ds;
}

From source file:opennlp.tools.similarity.apps.solr.NLProgram2CodeRequestHandler.java

License:Apache License

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {
    // get query string
    String requestExpression = req.getParamString();
    String[] exprParts = requestExpression.split("&");
    String[] text = new String[exprParts.length];
    int count = 0;
    for (String val : exprParts) {
        if (val.startsWith("line=")) {
            val = StringUtils.mid(val, 5, val.length());
            text[count] = val;
            count++;//ww  w .  j  av  a2 s  . c  o m
        }

    }

    StringBuffer buf = new StringBuffer();
    for (String sent : text) {
        ObjectPhraseListForSentence opls = null;
        try {
            opls = compiler.convertSentenceToControlObjectPhrase(sent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(sent + "\n" + opls + "\n");
        buf.append(sent + "\n |=> " + opls + "\n");
    }

    LOG.info("re-ranking results: " + buf.toString());
    NamedList<Object> values = rsp.getValues();
    values.remove("response");
    values.add("response", buf.toString().trim());
    rsp.setAllValues(values);

}

From source file:opennlp.tools.similarity.apps.solr.SearchResultsReRankerRequestHandler.java

License:Apache License

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {
    // get query string
    String requestExpression = req.getParamString();
    String[] exprParts = requestExpression.split("&");
    for (String part : exprParts) {
        if (part.startsWith("q="))
            requestExpression = part;/*from   w ww  . jav  a 2  s .  c o m*/
    }
    String query = StringUtils.substringAfter(requestExpression, ":");
    LOG.info(requestExpression);

    SolrParams ps = req.getOriginalParams();
    Iterator<String> iter = ps.getParameterNamesIterator();
    List<String> keys = new ArrayList<String>();
    while (iter.hasNext()) {
        keys.add(iter.next());
    }

    List<HitBase> searchResults = new ArrayList<HitBase>();

    for (Integer i = 0; i < MAX_SEARCH_RESULTS; i++) {
        String title = req.getParams().get("t" + i.toString());
        String descr = req.getParams().get("d" + i.toString());

        if (title == null || descr == null)
            continue;

        HitBase hit = new HitBase();
        hit.setTitle(title);
        hit.setAbstractText(descr);
        hit.setSource(i.toString());
        searchResults.add(hit);
    }

    /*
     * http://173.255.254.250:8983/solr/collection1/reranker/?
     * q=search_keywords:design+iphone+cases&fields=spend+a+day+with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+fresh+design+with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+with+mobile+case+for+your+family&fields=Add+style+to+your+iPhone+and+iPad&fields=Add+Apple+fashion+to+your+iPhone+and+iPad
     * 
     */

    if (searchResults.size() < 1) {
        int count = 0;
        for (String val : exprParts) {
            if (val.startsWith("fields=")) {
                val = StringUtils.mid(val, 7, val.length());
                HitBase hit = new HitBase();
                hit.setTitle("");
                hit.setAbstractText(val);
                hit.setSource(new Integer(count).toString());
                searchResults.add(hit);
                count++;
            }

        }
    }

    List<HitBase> reRankedResults = null;
    query = query.replace('+', ' ');
    if (tooFewKeywords(query) || orQuery(query)) {
        reRankedResults = searchResults;
        LOG.info("No re-ranking for " + query);
    } else
        reRankedResults = calculateMatchScoreResortHits(searchResults, query);
    /*
     * <scores>
    <score index="2">3.0005</score>
    <score index="1">2.101</score>
    <score index="3">2.1003333333333334</score>
    <score index="4">2.00025</score>
    <score index="5">1.1002</score>
    </scores>
     * 
     * 
     */
    StringBuffer buf = new StringBuffer();
    buf.append("<scores>");
    for (HitBase hit : reRankedResults) {
        buf.append("<score index=\"" + hit.getSource() + "\">" + hit.getGenerWithQueryScore() + "</score>");
    }
    buf.append("</scores>");

    NamedList<Object> scoreNum = new NamedList<Object>();
    for (HitBase hit : reRankedResults) {
        scoreNum.add(hit.getSource(), hit.getGenerWithQueryScore());
    }

    StringBuffer bufNums = new StringBuffer();
    bufNums.append("order>");
    for (HitBase hit : reRankedResults) {
        bufNums.append(hit.getSource() + "_");
    }
    bufNums.append("/order>");

    LOG.info("re-ranking results: " + buf.toString());
    NamedList<Object> values = rsp.getValues();
    values.remove("response");
    values.add("response", scoreNum);
    //values.add("new_order", bufNums.toString().trim());
    rsp.setAllValues(values);

}

From source file:opennlp.tools.similarity.apps.solr.SearchResultsReRankerStanfRequestHandler.java

License:Apache License

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {
    // get query string
    String requestExpression = req.getParamString();
    String[] exprParts = requestExpression.split("&");
    for (String part : exprParts) {
        if (part.startsWith("q="))
            requestExpression = part;// w  w w.  j av a2 s  .c  om
    }
    String query = StringUtils.substringAfter(requestExpression, ":");
    LOG.info(requestExpression);

    SolrParams ps = req.getOriginalParams();
    Iterator<String> iter = ps.getParameterNamesIterator();
    List<String> keys = new ArrayList<String>();
    while (iter.hasNext()) {
        keys.add(iter.next());
    }

    List<HitBase> searchResults = new ArrayList<HitBase>();

    for (Integer i = 0; i < MAX_SEARCH_RESULTS; i++) {
        String title = req.getParams().get("t" + i.toString());
        String descr = req.getParams().get("d" + i.toString());

        if (title == null || descr == null)
            continue;

        HitBase hit = new HitBase();
        hit.setTitle(title);
        hit.setAbstractText(descr);
        hit.setSource(i.toString());
        searchResults.add(hit);
    }

    /*
     * http://173.255.254.250:8983/solr/collection1/reranker/?
     * q=search_keywords:design+iphone+cases&fields=spend+a+day+with+a+
     * custom+iPhone+case&fields=Add+style+to+your+every+day+fresh+design+
     * with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+with+
     * mobile+case+for+your+family&fields=Add+style+to+your+iPhone+and+iPad&
     * fields=Add+Apple+fashion+to+your+iPhone+and+iPad
     * 
     */

    if (searchResults.size() < 1) {
        int count = 0;
        for (String val : exprParts) {
            if (val.startsWith("fields=")) {
                val = StringUtils.mid(val, 7, val.length());
                HitBase hit = new HitBase();
                hit.setTitle("");
                hit.setAbstractText(val);
                hit.setSource(new Integer(count).toString());
                searchResults.add(hit);
                count++;
            }

        }
    }

    List<HitBase> reRankedResults = null;
    query = query.replace('+', ' ');
    if (tooFewKeywords(query) || orQuery(query)) {
        reRankedResults = searchResults;
        LOG.info("No re-ranking for " + query);
    } else
        reRankedResults = calculateMatchScoreResortHits(searchResults, query);
    /*
     * <scores> <score index="2">3.0005</score> <score
     * index="1">2.101</score> <score index="3">2.1003333333333334</score>
     * <score index="4">2.00025</score> <score index="5">1.1002</score>
     * </scores>
     * 
     * 
     */
    StringBuffer buf = new StringBuffer();
    buf.append("<scores>");
    for (HitBase hit : reRankedResults) {
        buf.append("<score index=\"" + hit.getSource() + "\">" + hit.getGenerWithQueryScore() + "</score>");
    }
    buf.append("</scores>");

    NamedList<Object> scoreNum = new NamedList<Object>();
    for (HitBase hit : reRankedResults) {
        scoreNum.add(hit.getSource(), hit.getGenerWithQueryScore());
    }

    StringBuffer bufNums = new StringBuffer();
    bufNums.append("order>");
    for (HitBase hit : reRankedResults) {
        bufNums.append(hit.getSource() + "_");
    }
    bufNums.append("/order>");

    LOG.info("re-ranking results: " + buf.toString());
    NamedList<Object> values = rsp.getValues();
    values.remove("response");
    values.add("response", scoreNum);
    values.add("new_order", bufNums.toString().trim());
    rsp.setAllValues(values);

}

From source file:opennlp.tools.similarity.apps.solr.SyntGenRequestHandler.java

License:Apache License

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {
    try {/*  www  . j  a va  2  s.  c  om*/
        super.handleRequestBody(req, rsp);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    SolrParams reqValues = req.getOriginalParams();
    Iterator<String> iter = reqValues.getParameterNamesIterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }

    String param = req.getParamString();
    //modify rsp
    NamedList values = rsp.getValues();
    ResultContext c = (ResultContext) values.get("response");
    if (c == null)
        return;

    String val1 = (String) values.get("t1");
    String k1 = values.getName(0);
    k1 = values.getName(1);
    k1 = values.getName(2);
    k1 = values.getName(3);
    k1 = values.getName(4);

    DocList dList = c.docs;
    DocList dListResult = null;
    try {
        dListResult = filterResultsBySyntMatchReduceDocSet(dList, req, req.getParams());
    } catch (Exception e) {
        dListResult = dList;
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    c.docs = dListResult;
    values.remove("response");

    rsp.setAllValues(values);
}

From source file:org.alfresco.solr.AbstractAlfrescoSolrTests.java

License:Open Source License

/**
 * Validates a query matches some XPath test expressions and closes the query
 * @param message//w w w  .  j  av a  2s.c om
 * @param req
 * @param tests
 */
public static void assertQ(String message, SolrQueryRequest req, String... tests) {
    try {
        String response = query(req);
        if (req.getParams().getBool("facet", false)) {
            // add a test to ensure that faceting did not throw an exception
            // internally, where it would be added to facet_counts/exception
            String[] allTests = new String[tests.length + 1];
            System.arraycopy(tests, 0, allTests, 1, tests.length);
            allTests[0] = "*[count(//lst[@name='facet_counts']/*[@name='exception'])=0]";
            tests = allTests;
        }
        String results = BaseTestHarness.validateXPath(response, tests);

        if (null != results) {
            String msg = "REQUEST FAILED: xpath=" + results + "\n\txml response was: " + response
                    + "\n\trequest was:" + req.getParamString();
            LOG.error(msg);
            throw new RuntimeException(msg);
        }
    } catch (XPathExpressionException e1) {
        throw new RuntimeException("XPath is invalid", e1);
    } catch (Exception e2) {
        throw new RuntimeException("Exception during query", e2);
    }
}

From source file:org.solbase.SolbaseDispatchFilter.java

License:Apache License

@SuppressWarnings({ "unused", "unchecked" })
private void handleAdminRequest(HttpServletRequest req, ServletResponse response, SolrRequestHandler handler,
        SolrQueryRequest solrReq) throws IOException {
    SolrQueryResponse solrResp = new SolrQueryResponse();
    final NamedList<Object> responseHeader = new SimpleOrderedMap<Object>();
    solrResp.add("responseHeader", responseHeader);
    NamedList<Object> toLog = solrResp.getToLog();
    toLog.add("webapp", req.getContextPath());
    toLog.add("path", solrReq.getContext().get("path"));
    toLog.add("params", "{" + solrReq.getParamString() + "}");
    handler.handleRequest(solrReq, solrResp);
    SolrCore.setResponseHeaderValues(handler, solrReq, solrResp);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < toLog.size(); i++) {
        String name = toLog.getName(i);
        Object val = toLog.getVal(i);
        sb.append(name).append("=").append(val).append(" ");
    }/* www  .j a  v a  2 s.  c  o  m*/
    QueryResponseWriter respWriter = SolrCore.DEFAULT_RESPONSE_WRITERS
            .get(solrReq.getParams().get(CommonParams.WT));
    if (respWriter == null)
        respWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get("standard");
    writeResponse(solrResp, response, respWriter, solrReq, Method.getMethod(req.getMethod()));
}