Example usage for org.apache.solr.client.solrj SolrQuery setParam

List of usage examples for org.apache.solr.client.solrj SolrQuery setParam

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setParam.

Prototype

public SolrQuery setParam(String name, boolean value) 

Source Link

Usage

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedMatchesSource.java

License:Open Source License

@Override
public String getMarkup() {
    init(); //inits once

    String highLightField = null;

    String highlightQuery = keywordHitQuery;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
        //escape special lucene chars if not already escaped (if not a compound query)
        //TODO a better way to mark it a compound highlight query
        final String findSubstr = LuceneQuery.HIGHLIGHT_FIELD_REGEX + ":";
        if (!highlightQuery.contains(findSubstr)) {
            highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
        }//  ww  w.ja  va2s.  com
    } else {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
        //escape special lucene chars always for literal queries query
        highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    String queryStr = null;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highLightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(highlightQuery);
        if (group) {
            sb.append("\"");
        }
        queryStr = sb.toString();
    } else {
        //use default field, simplifies query
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(highlightQuery);
    }

    q.setQuery(queryStr);

    final long contentId = content.getId();

    String contentIdStr = Long.toString(contentId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":" + contentIdStr;
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED);

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NO_MATCHES;
        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NO_MATCHES;
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>";
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Couldn't query markup for page: " + currentPage, ex);
        return "";
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Could not query markup for page: " + currentPage, ex);
        return "";
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedText.java

License:Open Source License

@Override
public String getText() {
    loadPageInfo(); //inits once

    String highLightField = null;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
    } else {/*from   w w  w.  j ava2s . co  m*/
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    // input query has already been properly constructed and escaped
    q.setQuery(keywordHitQuery);

    String contentIdStr = Long.toString(this.objectId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":"
            + KeywordSearchUtil.escapeLuceneQuery(contentIdStr);
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>"; //NON-NLS
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error executing Solr highlighting query: " + keywordHitQuery, ex); //NON-NLS
        return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.queryFailedMsg");
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedTextMarkup.java

License:Open Source License

@Override
public String getMarkup() {
    loadPageInfo(); //inits once

    String highLightField = null;

    String highlightQuery = keywordHitQuery;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
        //escape special lucene chars if not already escaped (if not a compound query)
        //TODO a better way to mark it a compound highlight query
        final String findSubstr = LuceneQuery.HIGHLIGHT_FIELD_REGEX + ":";
        if (!highlightQuery.contains(findSubstr)) {
            highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
        }/*w w w  . jav  a2 s .com*/
    } else {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
        //escape special lucene chars always for literal queries query
        highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    String queryStr = null;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highLightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(highlightQuery);
        if (group) {
            sb.append("\"");
        }
        queryStr = sb.toString();
    } else {
        //use default field, simplifies query
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(highlightQuery);
    }

    q.setQuery(queryStr);

    String contentIdStr = Long.toString(this.objectId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":"
            + KeywordSearchUtil.escapeLuceneQuery(contentIdStr);
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");

        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>"; //NON-NLS
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Couldn't query markup for page: " + currentPage, ex); //NON-NLS
        return "";
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Could not query markup for page: " + currentPage, ex); //NON-NLS
        return "";
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.LuceneQuery.java

License:Open Source License

/**
 * Create the query object for the stored keyword
 *
 * @param snippets True if query should request snippets
 * @return//from w ww .  j ava  2  s . c  om
 */
private SolrQuery createAndConfigureSolrQuery(boolean snippets) {
    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug
    //set query, force quotes/grouping around all literal queries
    final String groupedQuery = KeywordSearchUtil.quoteQuery(keywordStringEscaped);
    String theQueryStr = groupedQuery;
    if (field != null) {
        //use the optional field
        StringBuilder sb = new StringBuilder();
        sb.append(field).append(":").append(groupedQuery);
        theQueryStr = sb.toString();
    }
    q.setQuery(theQueryStr);
    q.setRows(MAX_RESULTS);

    if (snippets) {
        q.setFields(Server.Schema.ID.toString());
    } else {
        q.setFields(Server.Schema.ID.toString());
    }

    for (KeywordQueryFilter filter : filters) {
        q.addFilterQuery(filter.toString());
    }

    if (snippets) {
        q.addHighlightField(Server.Schema.TEXT.toString());
        //q.setHighlightSimplePre("&laquo;"); //original highlighter only
        //q.setHighlightSimplePost("&raquo;");  //original highlighter only
        q.setHighlightSnippets(1);
        q.setHighlightFragsize(SNIPPET_LENGTH);

        //tune the highlighter
        q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
        q.setParam("hl.tag.pre", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
        q.setParam("hl.tag.post", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
        q.setParam("hl.fragListBuilder", "simple"); //makes sense for FastVectorHighlighter only NON-NLS

        //Solr bug if fragCharSize is smaller than Query string, StringIndexOutOfBoundsException is thrown.
        q.setParam("hl.fragCharSize", Integer.toString(theQueryStr.length())); //makes sense for FastVectorHighlighter only NON-NLS

        //docs says makes sense for the original Highlighter only, but not really
        //analyze all content SLOW! consider lowering
        q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS
    }

    return q;
}

From source file:org.sleuthkit.autopsy.keywordsearch.LuceneQuery.java

License:Open Source License

/**
 * return snippet preview context// w  ww.j av  a 2s.  c  o m
 *
 * @param query the keyword query for text to highlight. Lucene special
 * cahrs should already be escaped.
 * @param solrObjectId Solr object id associated with the hit
 * @param chunkID chunk id associated with the content hit, or 0 if no
 * chunks
 * @param isRegex whether the query is a regular expression (different Solr
 * fields are then used to generate the preview)
 * @param group whether the query should look for all terms grouped together
 * in the query order, or not
 * @return
 */
public static String querySnippet(String query, long solrObjectId, int chunkID, boolean isRegex, boolean group)
        throws NoOpenCoreException {
    Server solrServer = KeywordSearch.getServer();

    String highlightField;
    if (isRegex) {
        highlightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
    } else {
        highlightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
    }

    SolrQuery q = new SolrQuery();

    String queryStr;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highlightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(query);
        if (group) {
            sb.append("\"");
        }

        queryStr = sb.toString();
    } else {
        //simplify query/escaping and use default field
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(query);
    }

    q.setQuery(queryStr);

    String contentIDStr;

    if (chunkID == 0) {
        contentIDStr = Long.toString(solrObjectId);
    } else {
        contentIDStr = Server.getChunkIdString(solrObjectId, chunkID);
    }

    String idQuery = Server.Schema.ID.toString() + ":" + KeywordSearchUtil.escapeLuceneQuery(contentIDStr);
    q.setShowDebugInfo(DEBUG); //debug
    q.addFilterQuery(idQuery);
    q.addHighlightField(highlightField);
    //q.setHighlightSimplePre("&laquo;"); //original highlighter only
    //q.setHighlightSimplePost("&raquo;");  //original highlighter only
    q.setHighlightSnippets(1);
    q.setHighlightFragsize(SNIPPET_LENGTH);

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "simple"); //makes sense for FastVectorHighlighter only NON-NLS

    //Solr bug if fragCharSize is smaller than Query string, StringIndexOutOfBoundsException is thrown.
    q.setParam("hl.fragCharSize", Integer.toString(queryStr.length())); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    //analyze all content SLOW! consider lowering
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();
        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIDStr);
        if (responseHighlightID == null) {
            return "";
        }
        List<String> contentHighlights = responseHighlightID.get(highlightField);
        if (contentHighlights == null) {
            return "";
        } else {
            // extracted content is HTML-escaped, but snippet goes in a plain text field
            return EscapeUtil.unEscapeHtml(contentHighlights.get(0)).trim();
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + query, ex); //NON-NLS
        throw ex;
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + query, ex); //NON-NLS
        return "";
    }
}

From source file:org.xwiki.platform.search.solr.internal.SolrSearchService.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww w.j  a  v a  2 s .  co m*/
 * 
 * @see org.xwiki.platform.search.SearchService#queryDocument(java.lang.String)
 */
@Override
public ArrayList<String> queryDocument(String query, String qfvalues) {
    SolrQuery squery = new SolrQuery();
    squery.setQuery(query).setFacet(true).addFacetField("title_en", "title_fr", "title_es", "ft_en", "ft_fr",
            "ft_es");

    // Edismax
    squery.setParam("defType", "edismax");
    squery.setParam("qt", "edismax");

    // qf values passed on from front end.
    squery.setParam("qf", qfvalues);
    squery.setParam("debugQuery", "on");

    // Highlighting
    squery.setHighlight(true);
    squery.setParam("hl.simple.pre", "**");
    squery.setParam("hl.simple.post", "**");

    logger.info("Querying document - " + query);

    QueryResponse response;
    Map<String, Map<String, List<String>>> highlightingMap = null;
    try {

        response = solrServer.query(squery);
        logger.info("Facet fields " + response.getFacetFields());

        ArrayList<String> docNames = new ArrayList<String>();
        highlightingMap = response.getHighlighting();
        logger.info("Highlighting :: " + highlightingMap);

        for (SolrDocument d : response.getResults()) {
            String language = "_" + (String) d.getFieldValue("language");
            logger.info("The language for the document is - " + language);

            String space = (String) d.getFieldValue(IndexFields.DOCUMENT_SPACE + language);
            String pageName = (String) d.getFieldValue(IndexFields.DOCUMENT_NAME + language);
            String title = (String) d.getFieldValue(IndexFields.DOCUMENT_TITLE + language);
            Float score = (Float) d.getFieldValue("score");
            String id = (String) d.getFieldValue("id");

            if (highlightingMap != null) {
                Map<String, List<String>> docMap = highlightingMap.get(id);
                if (docMap != null && docMap.containsKey(IndexFields.DOCUMENT_TITLE + language)) {
                    title = docMap.get(IndexFields.DOCUMENT_TITLE + language).toString();
                }
            }

            // return a string with link to the page, score and page title ( varies from language to langauge for
            // same page).
            String retStr = "[[" + space + "." + pageName + "]] with a score (" + score + ") \\\\" + title;

            logger.info("Fields :: " + retStr);
            docNames.add(retStr);
        }
        return docNames;
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

    return (ArrayList<String>) Collections.EMPTY_LIST;

}

From source file:peltomaa.sukija.Query.java

License:Open Source License

public void query(String text) {
    try {/*w  w w.  j  a  v a 2s. c o m*/
        SolrQuery query = new SolrQuery();
        query.setQuery(text);
        query.setParam("rows", "1000");
        query.addTermsField("text");
        query.setTerms(true);
        query.setParam("hl.useFastVectorHighlighter", "true");
        query.setParam("hl.mergeContiguous", "true");
        //    query.setParam ("hl.fragsize", "0");
        query.setParam("hl.maxAnalyzedChars", "-1");

        // To choose a different request handler, for example, just set the qt parameter.
        query.set("qt", "/sukija");
        QueryResponse response = server.query(query);

        //    for (String s : query.getTermsFields()) System.out.println ("Field " + s);
        //    QueryResponsePrinter.print (System.out, response);

        SolrDocumentList documents = response.getResults();

        if (documents == null) {
            messageField.setText("Sukija.java: documents == null: ohjelmassa on virhe.");
            System.exit(1);
        }

        setMessage(documents.size());

        if (documents.size() == 0) {
            editorPane.setText("");
        } else {
            editorPane.setText(getText(response));
            editorPane.setCaretPosition(0);
        }
    } catch (SolrServerException ex) {
        messageField.setText(ex.getMessage());
    } catch (Exception ex) {
        messageField.setText(ex.getMessage());
    }
}

From source file:podd.search.impl.IndexSearcherImpl.java

License:Open Source License

/**
 * Creates a new SolrQuery with some predefined parameters
 * @return//from   w  w  w  . j av a 2s .  c o  m
 */
private SolrQuery createBasicSolrQuery() {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setHighlight(true).setHighlightSnippets(1); //set other params as needed       
    solrQuery.setParam("hl.fl", "*");

    return solrQuery;
}

From source file:uk.ac.ebi.atlas.solr.query.builders.BioentityIdentifierQueryBuilder.java

License:Apache License

private SolrQuery buildQueryObject(String queryString) {

    SolrQuery solrQuery = new SolrQuery(queryString);
    solrQuery.setFields(BIOENTITY_IDENTIFIER_FIELD);
    solrQuery.setParam("group", true);
    solrQuery.setParam("group.field", BIOENTITY_IDENTIFIER_FIELD);
    solrQuery.setParam("group.main", true);
    solrQuery.setRows(MAX_GENE_IDS_TO_FETCH);

    LOGGER.trace("<buildQueryObject> solr query: " + solrQuery.toString());

    return solrQuery;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.ontology.OntologySearcher.java

License:Apache License

public Set<String> getOntologyNames() throws SolrServerException {
    if (ontologyNames == null) {
        SolrQuery query = new SolrQuery("*:*");
        query.setStart(0);//  w  w w.j av  a  2s  .c  om
        query.setRows(0);

        // prepare faceting
        query.setFacet(true);
        query.setParam(FacetParams.FACET_FIELD, "ontology");
        query.setFacetLimit(10);
        query.setFacetMinCount(1);
        query.set(FacetParams.FACET_OFFSET, 0);

        // order by unique id
        query.addSortField(OntologyFieldNames.ID, SolrQuery.ORDER.asc);

        QueryResponse response = search(query, null);
        FacetField facetField = response.getFacetField("ontology");

        if (facetField.getValues() == null) {
            return Collections.EMPTY_SET;
        }

        ontologyNames = new HashSet<String>(facetField.getValues().size());

        for (FacetField.Count c : facetField.getValues()) {
            ontologyNames.add(c.getName());
        }
    }

    return ontologyNames;
}