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

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

Introduction

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

Prototype

public SolrQuery setHighlightSimplePre(String f) 

Source Link

Usage

From source file:org.apache.jackrabbit.oak.plugins.index.solr.query.FilterQueryParser.java

License:Apache License

static SolrQuery getQuery(Filter filter, QueryIndex.IndexPlan plan, OakSolrConfiguration configuration) {

    SolrQuery solrQuery = new SolrQuery();
    setDefaults(solrQuery, configuration);

    StringBuilder queryBuilder = new StringBuilder();

    FullTextExpression ft = filter.getFullTextConstraint();
    if (ft != null) {
        queryBuilder.append(parseFullTextExpression(ft, configuration));
        queryBuilder.append(' ');
    } else if (filter.getFulltextConditions() != null) {
        Collection<String> fulltextConditions = filter.getFulltextConditions();
        for (String fulltextCondition : fulltextConditions) {
            queryBuilder.append(fulltextCondition).append(" ");
        }//  www.  j  a  v a  2 s.c om
    }

    List<QueryIndex.OrderEntry> sortOrder = plan.getSortOrder();
    if (sortOrder != null) {
        for (QueryIndex.OrderEntry orderEntry : sortOrder) {
            SolrQuery.ORDER order;
            if (QueryIndex.OrderEntry.Order.ASCENDING.equals(orderEntry.getOrder())) {
                order = SolrQuery.ORDER.asc;
            } else {
                order = SolrQuery.ORDER.desc;
            }
            String sortingField;
            if (JcrConstants.JCR_PATH.equals(orderEntry.getPropertyName())) {
                sortingField = partialEscape(configuration.getPathField()).toString();
            } else if (JcrConstants.JCR_SCORE.equals(orderEntry.getPropertyName())) {
                sortingField = "score";
            } else {
                if (orderEntry.getPropertyName().indexOf('/') >= 0) {
                    log.warn("cannot sort on relative properties, ignoring {} clause", orderEntry);
                    continue; // sorting by relative properties not supported until index time aggregation is supported
                }
                sortingField = partialEscape(
                        getSortingField(orderEntry.getPropertyType().tag(), orderEntry.getPropertyName()))
                                .toString();
            }
            solrQuery.addOrUpdateSort(sortingField, order);
        }
    }

    Collection<Filter.PropertyRestriction> propertyRestrictions = filter.getPropertyRestrictions();
    if (propertyRestrictions != null && !propertyRestrictions.isEmpty()) {
        for (Filter.PropertyRestriction pr : propertyRestrictions) {
            if (pr.isNullRestriction()) {
                // can not use full "x is null"
                continue;
            }
            // facets
            if (QueryImpl.REP_FACET.equals(pr.propertyName)) {
                solrQuery.setFacetMinCount(1);
                solrQuery.setFacet(true);
                String value = pr.first.getValue(Type.STRING);
                solrQuery.addFacetField(
                        value.substring(QueryImpl.REP_FACET.length() + 1, value.length() - 1) + "_facet");
            }

            // native query support
            if (SolrQueryIndex.NATIVE_SOLR_QUERY.equals(pr.propertyName)
                    || SolrQueryIndex.NATIVE_LUCENE_QUERY.equals(pr.propertyName)) {
                String nativeQueryString = String.valueOf(pr.first.getValue(pr.first.getType()));
                if (isSupportedHttpRequest(nativeQueryString)) {
                    // pass through the native HTTP Solr request
                    String requestHandlerString = nativeQueryString.substring(0,
                            nativeQueryString.indexOf('?'));
                    if (!"select".equals(requestHandlerString)) {
                        if (requestHandlerString.charAt(0) != '/') {
                            requestHandlerString = "/" + requestHandlerString;
                        }
                        solrQuery.setRequestHandler(requestHandlerString);
                    }
                    String parameterString = nativeQueryString.substring(nativeQueryString.indexOf('?') + 1);
                    for (String param : parameterString.split("&")) {
                        String[] kv = param.split("=");
                        if (kv.length != 2) {
                            throw new RuntimeException("Unparsable native HTTP Solr query");
                        } else {
                            // more like this
                            if ("/mlt".equals(requestHandlerString)) {
                                if ("stream.body".equals(kv[0])) {
                                    kv[0] = "q";
                                    String mltFlString = "mlt.fl=";
                                    int mltFlIndex = parameterString.indexOf(mltFlString);
                                    if (mltFlIndex > -1) {
                                        int beginIndex = mltFlIndex + mltFlString.length();
                                        int endIndex = parameterString.indexOf('&', beginIndex);
                                        String fields;
                                        if (endIndex > beginIndex) {
                                            fields = parameterString.substring(beginIndex, endIndex);
                                        } else {
                                            fields = parameterString.substring(beginIndex);
                                        }
                                        kv[1] = "_query_:\"{!dismax qf=" + fields + " q.op=OR}" + kv[1] + "\"";
                                    }
                                }
                                if ("mlt.fl".equals(kv[0]) && ":path".equals(kv[1])) {
                                    // rep:similar passes the path of the node to find similar documents for in the :path
                                    // but needs its indexed content to find similar documents
                                    kv[1] = configuration.getCatchAllField();
                                }
                            }
                            if ("/spellcheck".equals(requestHandlerString)) {
                                if ("term".equals(kv[0])) {
                                    kv[0] = "spellcheck.q";
                                }
                                solrQuery.setParam("spellcheck", true);
                            }
                            if ("/suggest".equals(requestHandlerString)) {
                                if ("term".equals(kv[0])) {
                                    kv[0] = "suggest.q";
                                }
                                solrQuery.setParam("suggest", true);
                            }
                            solrQuery.setParam(kv[0], kv[1]);
                        }
                    }
                    return solrQuery;
                } else {
                    queryBuilder.append(nativeQueryString);
                }
            } else {
                if (SolrQueryIndex.isIgnoredProperty(pr, configuration)) {
                    continue;
                }

                String first = null;
                if (pr.first != null) {
                    first = partialEscape(String.valueOf(pr.first.getValue(pr.first.getType()))).toString();
                }
                String last = null;
                if (pr.last != null) {
                    last = partialEscape(String.valueOf(pr.last.getValue(pr.last.getType()))).toString();
                }

                String prField = configuration.getFieldForPropertyRestriction(pr);
                CharSequence fieldName = partialEscape(prField != null ? prField : pr.propertyName);
                if ("jcr\\:path".equals(fieldName.toString())) {
                    queryBuilder.append(configuration.getPathField());
                    queryBuilder.append(':');
                    queryBuilder.append(first);
                } else {
                    if (pr.first != null && pr.last != null && pr.first.equals(pr.last)) {
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(first);
                    } else if (pr.first == null && pr.last == null) {
                        if (!queryBuilder.toString().contains(fieldName + ":")) {
                            queryBuilder.append(fieldName).append(':');
                            queryBuilder.append('*');
                        }
                    } else if ((pr.first != null && pr.last == null) || (pr.last != null && pr.first == null)
                            || (!pr.first.equals(pr.last))) {
                        // TODO : need to check if this works for all field types (most likely not!)
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(createRangeQuery(first, last, pr.firstIncluding, pr.lastIncluding));
                    } else if (pr.isLike) {
                        // TODO : the current parameter substitution is not expected to work well
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(partialEscape(String.valueOf(pr.first.getValue(pr.first.getType()))
                                .replace('%', '*').replace('_', '?')));
                    } else {
                        throw new RuntimeException("[unexpected!] not handled case");
                    }
                }
            }
            queryBuilder.append(" ");
        }
    }

    if (configuration.useForPrimaryTypes()) {
        String[] pts = filter.getPrimaryTypes().toArray(new String[filter.getPrimaryTypes().size()]);
        StringBuilder ptQueryBuilder = new StringBuilder();
        for (int i = 0; i < pts.length; i++) {
            String pt = pts[i];
            if (i == 0) {
                ptQueryBuilder.append("(");
            }
            if (i > 0 && i < pts.length) {
                ptQueryBuilder.append("OR ");
            }
            ptQueryBuilder.append("jcr\\:primaryType").append(':').append(partialEscape(pt)).append(" ");
            if (i == pts.length - 1) {
                ptQueryBuilder.append(")");
                ptQueryBuilder.append(' ');
            }
        }
        solrQuery.addFilterQuery(ptQueryBuilder.toString());
    }

    if (filter.getQueryStatement() != null && filter.getQueryStatement().contains(QueryImpl.REP_EXCERPT)) {
        if (!solrQuery.getHighlight()) {
            // enable highlighting
            solrQuery.setHighlight(true);
            // defaults
            solrQuery.set("hl.fl", "*");
            solrQuery.set("hl.encoder", "html");
            solrQuery.set("hl.mergeContiguous", true);
            solrQuery.setHighlightSimplePre("<strong>");
            solrQuery.setHighlightSimplePost("</strong>");
        }
    }

    if (configuration.useForPathRestrictions()) {
        Filter.PathRestriction pathRestriction = filter.getPathRestriction();
        if (pathRestriction != null) {
            String path = purgePath(filter, plan.getPathPrefix());
            String fieldName = configuration.getFieldForPathRestriction(pathRestriction);
            if (fieldName != null) {
                if (pathRestriction.equals(Filter.PathRestriction.ALL_CHILDREN)) {
                    solrQuery.addFilterQuery(fieldName + ':' + path);
                } else {
                    queryBuilder.append(fieldName);
                    queryBuilder.append(':');
                    queryBuilder.append(path);
                }
            }
        }
    }

    if (configuration.collapseJcrContentNodes()) {
        solrQuery.addFilterQuery("{!collapse field=" + configuration.getCollapsedPathField() + " min="
                + configuration.getPathDepthField() + " hint=top_fc nullPolicy=expand}");
    }

    if (queryBuilder.length() == 0) {
        queryBuilder.append("*:*");
    }
    String escapedQuery = queryBuilder.toString();
    solrQuery.setQuery(escapedQuery);

    if (log.isDebugEnabled()) {
        log.debug("JCR query {} has been converted to Solr query {}", filter.getQueryStatement(),
                solrQuery.toString());
    }

    return solrQuery;
}

From source file:org.apache.ofbiz.solr.SolrProductSearch.java

License:Apache License

/**
 * Runs a query on the Solr Search Engine and returns the results.
 * <p>//from  ww w .ja v a  2s. c o m
 * This function only returns an object of type QueryResponse, so it is probably not a good idea to call it directly from within the
 * groovy files (As a decent example on how to use it, however, use keywordSearch instead).
 */
public static Map<String, Object> runSolrQuery(DispatchContext dctx, Map<String, Object> context) {
    // get Connection
    HttpSolrClient client = null;
    String solrIndexName = (String) context.get("indexName");
    Map<String, Object> result;
    try {
        client = SolrUtil.getInstance().getHttpSolrClient(solrIndexName);
        // create Query Object
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery((String) context.get("query"));
        // solrQuery.setQueryType("dismax");
        boolean faceted = (Boolean) context.get("facet");
        if (faceted) {
            solrQuery.setFacet(faceted);
            solrQuery.addFacetField("manu");
            solrQuery.addFacetField("cat");
            solrQuery.setFacetMinCount(1);
            solrQuery.setFacetLimit(8);

            solrQuery.addFacetQuery("listPrice:[0 TO 50]");
            solrQuery.addFacetQuery("listPrice:[50 TO 100]");
            solrQuery.addFacetQuery("listPrice:[100 TO 250]");
            solrQuery.addFacetQuery("listPrice:[250 TO 500]");
            solrQuery.addFacetQuery("listPrice:[500 TO 1000]");
            solrQuery.addFacetQuery("listPrice:[1000 TO 2500]");
            solrQuery.addFacetQuery("listPrice:[2500 TO 5000]");
            solrQuery.addFacetQuery("listPrice:[5000 TO 10000]");
            solrQuery.addFacetQuery("listPrice:[10000 TO 50000]");
            solrQuery.addFacetQuery("listPrice:[50000 TO *]");
        }

        boolean spellCheck = (Boolean) context.get("spellcheck");
        if (spellCheck) {
            solrQuery.setParam("spellcheck", spellCheck);
        }

        boolean highLight = (Boolean) context.get("highlight");
        if (highLight) {
            solrQuery.setHighlight(highLight);
            solrQuery.setHighlightSimplePre("<span class=\"highlight\">");
            solrQuery.addHighlightField("description");
            solrQuery.setHighlightSimplePost("</span>");
            solrQuery.setHighlightSnippets(2);
        }

        // Set additional Parameter
        // SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (context.get("viewIndex") != null && (Integer) context.get("viewIndex") > 0) {
            solrQuery.setStart((Integer) context.get("viewIndex"));
        }
        if (context.get("viewSize") != null && (Integer) context.get("viewSize") > 0) {
            solrQuery.setRows((Integer) context.get("viewSize"));
        }

        // if ((List) context.get("queryFilter") != null && ((ArrayList<SolrDocument>) context.get("queryFilter")).size() > 0) {
        // List filter = (List) context.get("queryFilter");
        // String[] tn = new String[filter.size()];
        // Iterator it = filter.iterator();
        // for (int i = 0; i < filter.size(); i++) {
        // tn[i] = (String) filter.get(i);
        // }
        // solrQuery.setFilterQueries(tn);
        // }
        String queryFilter = (String) context.get("queryFilter");
        if (UtilValidate.isNotEmpty(queryFilter))
            solrQuery.setFilterQueries(queryFilter.split(" "));
        if ((String) context.get("returnFields") != null) {
            solrQuery.setFields((String) context.get("returnFields"));
        }

        // if((Boolean)context.get("sortByReverse"))order.reverse();
        if ((String) context.get("sortBy") != null && ((String) context.get("sortBy")).length() > 0) {
            SolrQuery.ORDER order;
            if (!((Boolean) context.get("sortByReverse")))
                order = SolrQuery.ORDER.asc;
            else
                order = SolrQuery.ORDER.desc;
            solrQuery.setSort(((String) context.get("sortBy")).replaceFirst("-", ""), order);
        }

        if ((String) context.get("facetQuery") != null) {
            solrQuery.addFacetQuery((String) context.get("facetQuery"));
        }

        QueryResponse rsp = client.query(solrQuery);
        result = ServiceUtil.returnSuccess();
        result.put("queryResult", rsp);
    } catch (Exception e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    } finally {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    return result;
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

/**
 * Parse the passed query./*from w  w  w  . ja  va2s .c om*/
 * 
 * @param query
 *        string
 * @return the parsed query
 * @throws ParseException
 *         when the parsing brakes
 */
@Override
protected Iterable<? extends DocumentScore> query(Resource subject, String query, URI propertyURI,
        boolean highlight) throws MalformedQueryException, IOException {
    SolrQuery q = prepareQuery(propertyURI, new SolrQuery(query));
    if (highlight) {
        q.setHighlight(true);
        String field = (propertyURI != null) ? SearchFields.getPropertyField(propertyURI) : "*";
        q.addHighlightField(field);
        q.setHighlightSimplePre(SearchFields.HIGHLIGHTER_PRE_TAG);
        q.setHighlightSimplePost(SearchFields.HIGHLIGHTER_POST_TAG);
        q.setHighlightSnippets(2);
    }

    QueryResponse response;
    if (q.getHighlight()) {
        q.addField("*");
    } else {
        q.addField(SearchFields.URI_FIELD_NAME);
    }
    q.addField("score");
    try {
        if (subject != null) {
            response = search(subject, q);
        } else {
            response = search(q);
        }
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
    SolrDocumentList results = response.getResults();
    final Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
    return Iterables.transform(results, new Function<SolrDocument, DocumentScore>() {

        @Override
        public DocumentScore apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            Map<String, List<String>> docHighlighting = (highlighting != null) ? highlighting.get(doc.getId())
                    : null;
            return new SolrDocumentScore(doc, docHighlighting);
        }
    });
}

From source file:org.emonocot.persistence.dao.hibernate.SearchableDaoImpl.java

License:Open Source License

public List<Match> autocomplete(final String query, Integer pageSize, Map<String, String> selectedFacets)
        throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();

    if (query != null && !query.trim().equals("")) {
        //String searchString = query.trim().replace(" ", "+");
        solrQuery.setQuery(query);/*www.  j  a  v a  2 s .c om*/
    } else {
        return new ArrayList<Match>();
    }

    // Filter the searchable objects out
    solrQuery.addFilterQuery("base.class_searchable_b:" + isSearchableObject());

    // Set additional result parameters
    //solrQuery.setRows(pageSize);
    int rows = 100;
    solrQuery.setRows(rows);

    if (selectedFacets != null && !selectedFacets.isEmpty()) {
        for (String facetName : selectedFacets.keySet()) {
            solrQuery.addFilterQuery(facetName + ":" + selectedFacets.get(facetName));
        }
    }

    solrQuery.set("defType", "edismax");
    solrQuery.set("qf", "autocomplete^3 autocompleteng");
    solrQuery.set("pf", "autocompletenge");
    solrQuery.set("fl", "autocomplete,id");
    solrQuery.setHighlight(true);
    solrQuery.set("hl.fl", "autocomplete");
    solrQuery.set("hl.snippets", 3);
    solrQuery.setHighlightSimplePre("<b>");
    solrQuery.setHighlightSimplePost("</b>");
    //solrQuery.setSortField("autocomplete", SolrQuery.ORDER.valueOf("desc"));
    /*
    solrQuery.set("group","true");
    solrQuery.set("group.field", "autocomplete");
     */

    QueryResponse queryResponse = solrServer.query(solrQuery);

    List<Match> results = new ArrayList<Match>();
    Map<String, Match> matchMap = new HashMap<String, Match>();

    for (SolrDocument solrDocument : queryResponse.getResults()) {
        Match match = new Match();
        String label = filter((String) solrDocument.get("autocomplete"));
        match.setLabel(label);
        match.setValue(label);
        matchMap.put((String) solrDocument.get("id"), match);
        results.add(match);
    }

    List<Match> distinctResults = removeDuplicates(results);

    List<Match> subResults1 = new ArrayList<Match>(); //ExactMatch
    List<Match> subResults2 = new ArrayList<Match>();

    for (Match item : distinctResults) {
        if ((item.getLabel().toLowerCase().startsWith(query.toLowerCase()))) {
            subResults1.add(item);
        } else {
            subResults2.add(item);
        }
    }

    if (subResults1.size() > 0) {
        Collections.sort(subResults1);
    }

    /*
    Collections.sort(subResults1, new Comparator() {
       @Override
       public int compare(Object matchOne, Object matchTwo) {
    //use instanceof to verify the references are indeed of the type in question
    return ((Match)matchOne).getLabel()
          .compareTo(((Match)matchTwo).getLabel());
       }
    });
    */

    subResults1.addAll(subResults2);
    List<Match> subResults = subResults1;

    List<Match> finalResults = new ArrayList<Match>();

    if (subResults.size() > 10) {
        finalResults = subResults.subList(0, 10);
    } else {
        finalResults = subResults;
    }

    //subResults = finalResults;

    /*
    for(GroupCommand groupCommand : queryResponse.getGroupResponse().getValues()) {
       for (Group group : groupCommand.getValues()) {
    for (SolrDocument solrDocument : group.getResult()) {
       Match match = new Match();
       String label = filter((String) solrDocument.get("autocomplete"));
       match.setLabel(label);
       match.setValue(label);
       matchMap.put((String) solrDocument.get("id"), match);
       results.add(match);
    }
       }
    }
    */
    for (String documentId : matchMap.keySet()) {
        if (queryResponse.getHighlighting().containsKey(documentId)) {
            Map<String, List<String>> highlightedTerms = queryResponse.getHighlighting().get(documentId);
            if (highlightedTerms.containsKey("autocomplete")) {
                matchMap.get(documentId).setLabel(highlightedTerms.get("autocomplete").get(0));
            }
        }
    }

    //return results;
    return finalResults;
}

From source file:org.ofbiz.solr.SolrProductSearch.java

License:Apache License

/**
 * Runs a query on the Solr Search Engine and returns the results.
 * <p>/*  www . j a v  a 2 s.  c o  m*/
 * This function only returns an object of type QueryResponse, so it is probably not a good idea to call it directly from within the
 * groovy files (As a decent example on how to use it, however, use keywordSearch instead).
 */
public static Map<String, Object> runSolrQuery(DispatchContext dctx, Map<String, Object> context) {
    // get Connection
    HttpSolrServer server = null;
    Map<String, Object> result;

    Integer viewIndex = (Integer) context.get("viewIndex");
    Integer viewSize = (Integer) context.get("viewSize");
    if (viewIndex < 1) {
        viewIndex = 1;
    }
    try {
        server = new HttpSolrServer(SolrUtil.solrUrl);
        // create Query Object
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery((String) context.get("query"));
        // solrQuery.setQueryType("dismax");
        boolean faceted = (Boolean) context.get("facet");
        if (faceted) {
            solrQuery.setFacet(faceted);
            //solrQuery.addFacetField("manu");
            solrQuery.addFacetField("features");
            solrQuery.addFacetField("cat");
            solrQuery.setFacetMinCount(1);
            solrQuery.setFacetLimit(8);

            solrQuery.addFacetQuery("listPrice:[0 TO 50]");
            solrQuery.addFacetQuery("listPrice:[50 TO 100]");
            solrQuery.addFacetQuery("listPrice:[100 TO 250]");
            solrQuery.addFacetQuery("listPrice:[250 TO 500]");
            solrQuery.addFacetQuery("listPrice:[500 TO 1000]");
            solrQuery.addFacetQuery("listPrice:[1000 TO 2500]");
            solrQuery.addFacetQuery("listPrice:[2500 TO 5000]");
            solrQuery.addFacetQuery("listPrice:[5000 TO 10000]");
            solrQuery.addFacetQuery("listPrice:[10000 TO 50000]");
            solrQuery.addFacetQuery("listPrice:[50000 TO *]");
        }

        boolean spellCheck = (Boolean) context.get("spellcheck");
        if (spellCheck) {
            solrQuery.setParam("spellcheck", spellCheck);
        }

        boolean highLight = (Boolean) context.get("highlight");
        if (highLight) {
            solrQuery.setHighlight(highLight);
            solrQuery.setHighlightSimplePre("<span class=\"highlight\">");
            solrQuery.addHighlightField("description");
            solrQuery.setHighlightSimplePost("</span>");
            solrQuery.setHighlightSnippets(2);
        }

        // Set additional Parameter
        // SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (viewIndex != null && viewIndex > 0) {
            solrQuery.setStart((viewIndex - 1) * viewSize);
        }
        if (viewSize != null && viewSize > 0) {
            solrQuery.setRows(viewSize);
        }

        // if ((List) context.get("queryFilter") != null && ((ArrayList<SolrDocument>) context.get("queryFilter")).size() > 0) {
        // List filter = (List) context.get("queryFilter");
        // String[] tn = new String[filter.size()];
        // Iterator it = filter.iterator();
        // for (int i = 0; i < filter.size(); i++) {
        // tn[i] = (String) filter.get(i);
        // }
        // solrQuery.setFilterQueries(tn);
        // }
        String queryFilter = (String) context.get("queryFilter");
        if (UtilValidate.isNotEmpty(queryFilter))
            solrQuery.setFilterQueries(queryFilter.split(" "));
        if ((String) context.get("returnFields") != null) {
            solrQuery.setFields((String) context.get("returnFields"));
        }

        // if((Boolean)context.get("sortByReverse"))order.reverse();
        if ((String) context.get("sortBy") != null && ((String) context.get("sortBy")).length() > 0) {
            SolrQuery.ORDER order;
            if (!((Boolean) context.get("sortByReverse")))
                order = SolrQuery.ORDER.asc;
            else
                order = SolrQuery.ORDER.desc;
            solrQuery.setSort(((String) context.get("sortBy")).replaceFirst("-", ""), order);
        }

        if ((String) context.get("facetQuery") != null) {
            solrQuery.addFacetQuery((String) context.get("facetQuery"));
        }

        QueryResponse rsp = server.query(solrQuery);
        result = ServiceUtil.returnSuccess();
        result.put("queryResult", rsp);
    } catch (Exception e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    }
    return result;
}

From source file:org.schedoscope.metascope.index.SolrQueryExecutor.java

License:Apache License

/**
 * Perform a query on the metascope solr index. Builds up the query string
 * from the given parameters and sends request to solr server
 * /*from w ww. j  a v a2 s.  com*/
 * @param params
 *          the parameters which are included in the filter query
 * @return a SolrQueryResult object containing the result of the query
 */
public SolrQueryResult query(Map<String, String> params) {
    SolrQuery query = new SolrQuery();

    /* enable facetting */
    query.setFacet(true);

    /* list of all filters; key: name to display, value: entity parameter */
    List<SolrQueryParameter> filter = new ArrayList<SolrQueryParameter>();

    /* list of partition filter */
    List<SolrQueryParameter> partitionFilter = new ArrayList<SolrQueryParameter>();

    /*
     * list of filter which have been set by the user (key = filtername, value =
     * list of selections)
     */
    Map<String, List<String>> activeFilterValues = new HashMap<String, List<String>>();

    /*
     * determine on which type is searched for (either 'Table' or 'Partition')
     */
    String typeFilterValue = params.get(TYPE);
    filter.add(new SolrQueryParameter(FILTER_TYPE, TYPE, FilterType.EXCLUSIVE, FacetSort.COUNT));
    if (typeFilterValue != null && typeFilterValue.equalsIgnoreCase(TYPE_PARTITION)) {
        typeFilterValue = TYPE_PARTITION;
        for (String parameter : fieldEntityService.findDistinctParameters()) {
            partitionFilter.add(new SolrQueryParameter("Parameter: " + parameter, parameter + "_s",
                    FilterType.AND, FacetSort.INDEX));
        }
        for (SolrQueryParameter pFilter : partitionFilter) {
            query.addFacetField(pFilter.getName());
            query.add("f." + pFilter.getName() + ".facet.sort", "index");
            String filterValue = params.get(pFilter.getName());
            if (filterValue != null && !filterValue.isEmpty()) {
                query.addFilterQuery(pFilter.getName() + ":" + "(" + filterValue.replace(",", " AND ") + ")");
            }

        }
    } else {
        typeFilterValue = TYPE_TABLE;
    }
    query.addFilterQuery("{!tag=" + TYPE + "}" + TYPE + ":" + typeFilterValue);
    query.addFacetField("{!ex=" + TYPE + "}" + TYPE);

    /* set solr search query parameter 'q' */
    String searchQueryValue = params.get(URLUtil.SEARCH_QUERY_PARAM);
    if (searchQueryValue == null || searchQueryValue.isEmpty()) {
        searchQueryValue = "*";
        query.setQuery(searchQueryValue);
    } else {
        String[] queryTerms = searchQueryValue.trim().split(" ");
        String queryTerm = "";
        for (String term : queryTerms) {
            if (term.isEmpty()) {
                continue;
            }

            if (!queryTerm.isEmpty()) {
                queryTerm += " AND ";
            }
            queryTerm += "*" + term + "*";
        }
        query.setQuery(queryTerm);
        query.setHighlight(true);
        query.setHighlightSimplePre("<b>");
        query.setHighlightSimplePost("</b>");
        query.setHighlightSnippets(100);
        query.set("hl.fl", "*");
    }

    /* set the remaining filters */
    for (SolrQueryParameter queryFilter : facetFields) {
        filter.add(queryFilter);
        String value = params.get(queryFilter.getName());

        String filterQuery = "";
        String facetField = "";
        if (queryFilter.isExcludeFromFacet()) {
            if (value != null) {
                String[] multipleFilter = value.split(",");
                value = "(";
                for (int i = 0; i < multipleFilter.length; i++) {
                    String filterValue = cleanseValue(multipleFilter[i]).replace(" ", "*");
                    value += "(" + filterValue + ")";
                    if (i < multipleFilter.length - 1) {
                        value += " " + getOperator(queryFilter.getFilterType()) + " ";
                    }
                }
                value += ")";
            }
            filterQuery = "{!tag=" + queryFilter.getName() + "}" + queryFilter.getName() + ":" + value;
            facetField = "{!ex=" + queryFilter.getName() + "}" + queryFilter.getName();
        } else {
            if (value != null) {
                String[] multipleFilter = value.split(",");
                value = "(";
                for (int i = 0; i < multipleFilter.length; i++) {
                    String filterValue = cleanseValue(multipleFilter[i]).replace(" ", "*");
                    value += "(" + filterValue + ")";
                    if (i < multipleFilter.length - 1) {
                        value += " " + getOperator(queryFilter.getFilterType()) + " ";
                    }
                }
                value += ")";
            }
            filterQuery = queryFilter.getName() + ":" + value;
            facetField = queryFilter.getName();
        }

        if (value != null && !value.isEmpty()) {
            query.addFilterQuery(filterQuery);
        }
        query.addFacetField(facetField);

        if (queryFilter.getFacetSort().equals(FacetSort.INDEX)) {
            query.add("f." + queryFilter.getName() + ".facet.sort", "index");
        }
        query.add("f." + queryFilter.getName() + ".facet.limit", "-1");
    }

    /* set facet queries */
    Map<String, String> facetQueryMap = new HashMap<String, String>();
    long now = System.currentTimeMillis() / 1000;
    for (SolrFacetQuery solrFacetQuery : facetQueries) {
        for (SolrHourRange range : solrFacetQuery.getRanges()) {
            long from = range.getFrom() == Long.MAX_VALUE ? 0 : now - (range.getFrom() * 3600);
            String facetQueryString = solrFacetQuery.getName() + ":[" + from + " TO " + now + "]";
            query.addFacetQuery("{!ex=" + solrFacetQuery.getName() + "}" + facetQueryString);
            facetQueryMap.put(solrFacetQuery.getName() + range.getName(), facetQueryString);
        }
        String value = params.get(solrFacetQuery.getName());
        if (value != null) {
            String fq = "{!tag=" + solrFacetQuery.getName() + "}"
                    + facetQueryMap.get(solrFacetQuery.getName() + value);
            query.addFilterQuery(fq);
        }
    }

    /* always sort the entities (for a deterministic view) */
    query.setSort(ID, ORDER.asc);

    /* set pagination information */
    int page = getPageParameter(params);
    int elements = getElementsParameter(params);
    query.setRows(elements);
    query.setStart((page - 1) * elements);

    /* execute the query */
    QueryResponse queryResponse = null;
    try {
        queryResponse = solrClient.query(query);
    } catch (Exception e) {
        e.printStackTrace();
    }

    SolrDocumentList list = queryResponse.getResults();

    /* get table / view entities from local repository */
    List<SolrQueryResultEntity> resultEntities = new LinkedList<SolrQueryResultEntity>();
    String resultType = "";
    for (SolrDocument solrDocument : list) {
        String id = (String) solrDocument.get(ID);

        if (typeFilterValue.equalsIgnoreCase(TYPE_PARTITION)) {
            if (!searchQueryValue.equals("*")) {
                resultEntities.add(new SolrQueryResultEntity(viewEntityService.findByUrlPath(id),
                        queryResponse.getHighlighting().get(id)));
            } else {
                resultEntities.add(new SolrQueryResultEntity(viewEntityService.findByUrlPath(id)));
            }
            resultType = TYPE_PARTITION;
        } else if (typeFilterValue.equalsIgnoreCase(TYPE_TABLE)) {
            if (!searchQueryValue.equals("*")) {
                resultEntities.add(new SolrQueryResultEntity(tableEntityService.findByFqdn(id),
                        queryResponse.getHighlighting().get(id)));
            } else {
                resultEntities.add(new SolrQueryResultEntity(tableEntityService.findByFqdn(id)));
            }
        }
    }
    if (resultType.isEmpty()) {
        resultType = TYPE_TABLE;
    }

    filter.addAll(partitionFilter);

    /* get the facet values and counts */
    Map<String, List<SolrFacet>> facetValues = new HashMap<String, List<SolrFacet>>();
    for (SolrQueryParameter f : filter) {
        if (!f.getName().equals(URLUtil.SEARCH_QUERY_PARAM)) {
            List<SolrFacet> values = new ArrayList<SolrFacet>();
            FacetField facet = queryResponse.getFacetField(f.getName());
            for (Count count : facet.getValues()) {
                values.add(new SolrFacet(count.getName(), count.getCount()));
            }
            facetValues.put(f.getName(), values);
        }
    }

    /* remove the type filter */
    filter.remove(0);

    for (SolrFacetQuery solrFacetQuery : facetQueries) {
        filter.add(new SolrQueryParameter(solrFacetQuery.getDisplayName(), solrFacetQuery.getName(),
                FilterType.EXCLUSIVE));
        List<SolrFacet> values = new ArrayList<SolrFacet>();
        for (SolrHourRange range : solrFacetQuery.getRanges()) {
            long facetQueryCount = getFacetQueryCount(queryResponse, "{!ex=" + solrFacetQuery.getName() + "}"
                    + facetQueryMap.get(solrFacetQuery.getName() + range.getName()));
            values.add(new SolrFacet(range.getName(), facetQueryCount));
        }
        facetValues.put(solrFacetQuery.getName(), values);
    }

    /* get the active filter values which have been selected by the user */
    addToActiveFilterValues(activeFilterValues, params, filter);

    /* build and return the result */
    SolrQueryResult result = new SolrQueryResult().withResultEntities(resultEntities).withResultType(resultType)
            .withFilters(filter).withFacetValues(facetValues).withActiveFilterValues(activeFilterValues)
            .withSearchQuery(searchQueryValue).withPage(page).withElements(elements)
            .withTotalPages((int) Math.ceil(((double) list.getNumFound()) / elements))
            .withTotalResults(list.getNumFound());
    return result;
}

From source file:org.segrada.search.solr.SolrSearchEngine.java

License:Apache License

@Override
public PaginationInfo<SearchHit> search(String searchTerm, Map<String, String> filters) {
    // to avoid NPEs
    if (filters == null)
        filters = new HashMap<>();

    // set defaults
    int page = 1;
    int entriesPerPage = 20;

    try {/*from   w ww . jav  a  2s  .  c  o  m*/
        // Parse a simple query that searches for "text":
        MultiFieldQueryParser parser;
        String[] containFields;
        // do we have a filter to contain to certain fields?
        if (filters.containsKey("fields")) {
            String fields = filters.get("fields");
            if (fields.isEmpty())
                containFields = new String[] { this.title, this.subTitles, this.content };
            else if (fields.equalsIgnoreCase(this.title))
                containFields = new String[] { this.title };
            else if (fields.equalsIgnoreCase(this.subTitles))
                containFields = new String[] { this.subTitles };
            else if (fields.equalsIgnoreCase(this.content))
                containFields = new String[] { this.content };
            else if (fields.equalsIgnoreCase("allTitles"))
                containFields = new String[] { this.title, this.subTitles };
            else
                throw new RuntimeException("fields-Filter " + fields + " is not known.");
        } else
            containFields = new String[] { this.title, this.subTitles, this.content };
        parser = new MultiFieldQueryParser(Version.LUCENE_47, containFields, analyzer);

        // which operator do we use?
        parser.setDefaultOperator(QueryParser.Operator.AND);
        if (filters.containsKey("operator")) {
            String operator = filters.get("operator");
            if (operator.equalsIgnoreCase("or"))
                parser.setDefaultOperator(QueryParser.Operator.OR);
            else if (!operator.isEmpty() && !operator.equalsIgnoreCase("and"))
                throw new RuntimeException("operator-Filter " + operator + " is not and/or.");
        }

        // filters for query
        SolrQuery query = new SolrQuery();
        // class filter
        if (filters.containsKey("class") && !filters.get("class").isEmpty()) {
            // multiple classes?
            String[] classes = filters.get("class").split(",");

            // single class
            if (classes.length <= 1) {
                query.addFilterQuery(this.className, filters.get("class"));
            } else { // multiple classes
                StringBuilder chained = new StringBuilder("(");
                for (int i = 0; i < classes.length; i++) {
                    if (i > 0)
                        chained.append(" OR ");
                    chained.append("className:").append(classes[i].trim());
                }
                query.addFilterQuery(this.className, chained + ")");
            }
        }

        // tag filter
        if (filters.containsKey("tags") && !filters.get("tags").isEmpty()) {
            // split tags into array
            String[] tags = filters.get("tags").split(",");
            BooleanQuery booleanQuery = new BooleanQuery();
            for (String tagLocal : tags) {
                booleanQuery.add(new TermQuery(new Term("tag", tagLocal.trim())), BooleanClause.Occur.SHOULD);
            }
            query.addFilterQuery(this.tag, booleanQuery.toString());
        }

        // define query
        Query queryTerm = null;
        if (searchTerm != null)
            queryTerm = parser.parse(searchTerm);
        if (queryTerm == null)
            queryTerm = new MatchAllDocsQuery(); // fallback to match all documents
        query.setQuery(queryTerm.toString());

        // get hits per page
        if (filters.containsKey("limit")) {
            try {
                entriesPerPage = Integer.valueOf(filters.get("limit"));
                if (entriesPerPage <= 0 || entriesPerPage > 1000)
                    entriesPerPage = 20;
            } catch (NumberFormatException e) {
                logger.warn("Could not parse limit " + filters.get("limit") + " to integer", e);
            }
        }

        // get page number
        if (filters.containsKey("page")) {
            try {
                page = Integer.valueOf(filters.get("page"));
            } catch (NumberFormatException e) {
                logger.warn("Could not parse page " + filters.get("page") + " to integer", e);
            }
        }

        // calculate start index
        int startIndex = (page - 1) * entriesPerPage;

        query.setStart(startIndex);
        query.setRows(entriesPerPage);

        query.setFields("*", "score");

        // define highlighting
        query.setHighlight(true);
        query.addHighlightField(this.content);
        query.setHighlightFragsize(18);
        query.setHighlightSnippets(10);
        query.setHighlightSimplePre("<b>");
        query.setHighlightSimplePost("</b>");

        // do query
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        // how many pages do we have?
        int pages = (int) (results.getNumFound() / entriesPerPage + 1);

        // cycle trough hits
        List<SearchHit> hits = new ArrayList<>();

        for (SolrDocument doc : results) {
            SearchHit searchHit = createHitFromDocument(doc);

            // add score
            Object score = doc.get("score");
            if (score != null && score instanceof Float)
                searchHit.setRelevance((float) score);

            // get highlighted components
            if (searchTerm != null && response.getHighlighting().get(searchHit.getId()) != null) {
                List<String> fragments = response.getHighlighting().get(searchHit.getId()).get(this.content);
                if (fragments != null) {
                    String[] bestFragments = new String[fragments.size() > 10 ? 10 : fragments.size()];
                    for (int i = 0; i < bestFragments.length; i++)
                        bestFragments[i] = fragments.get(i);
                    searchHit.setHighlightText(bestFragments);
                }
            }

            // add hit
            hits.add(searchHit);
        }

        // return pagination info
        return new PaginationInfo<>(page, pages, (int) results.getNumFound(), entriesPerPage, hits);
    } catch (Throwable e) {
        logger.error("Error in search.", e);
    }

    // return empty list result in order to avoid NPEs
    return new PaginationInfo<>(page, 1, 0, entriesPerPage, new ArrayList<>());
}

From source file:org.segrada.search.solr.SolrSearchEngine.java

License:Apache License

@Override
public String[] searchInDocument(String searchTerm, String id) {
    // sanity check
    if (searchTerm == null || id == null || searchTerm.isEmpty() || id.isEmpty())
        return new String[] {};

    try {/*ww w .  j ava2s . c om*/
        // only search content
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_47, new String[] { "content" },
                analyzer);

        SolrQuery query = new SolrQuery();

        // set operator and contain by id
        parser.setDefaultOperator(QueryParser.Operator.AND);
        query.setQuery(parser.parse(searchTerm).toString());

        // filter by id
        query.addFilterQuery("id:" + id);
        query.setRows(1);

        // define highlighting
        query.setHighlight(true);
        query.addHighlightField(this.content);
        query.setHighlightFragsize(100);
        query.setHighlightSnippets(100);
        query.setHighlightSimplePre("<b>");
        query.setHighlightSimplePost("</b>");

        // do query
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        if (!results.isEmpty() && response.getHighlighting().get(id) != null) {
            List<String> fragments = response.getHighlighting().get(id).get(this.content);
            String[] bestFragments = new String[fragments.size() > 100 ? 100 : fragments.size()];
            for (int i = 0; i < bestFragments.length; i++)
                bestFragments[i] = fragments.get(i);
            return bestFragments;
        }
    } catch (Throwable e) {
        logger.error("Error in search.", e);
    }

    return new String[] {};
}