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

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

Introduction

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

Prototype

public SolrQuery() 

Source Link

Usage

From source file:com.zb.app.external.lucene.solr.utils.BaseSolrQueryConvert.java

License:Open Source License

public static SolrQuery createSuggestQuery(SuggestQuery query) {
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder sb = new StringBuilder();
    sb.append("suggest:").append(query.getPrefix()).append("*");
    solrQuery.setQuery(sb.toString());//from  ww w  .  ja  v  a 2 s.co m
    solrQuery.addField(query.getField());
    if (StringUtils.isNotEmpty(query.getSortFiled())) {
        solrQuery.addSort(query.getSortFiled(), SolrQuery.ORDER.desc);
    }
    solrQuery.setStart(0);
    solrQuery.setRows(100);

    // solrQuery.set("qt", "/terms");
    // solrQuery.setFacet(true);
    // solrQuery.setFacetMinCount(1);
    // solrQuery.setFacetLimit(10);
    // solrQuery.setFacetPrefix(query.getField(), query.getPrefix());
    // solrQuery.setFacetSort(query.getSortFiled());
    // solrQuery.setRows(0);
    // solrQuery.setQuery("*");

    // solrQuery.setTerms(true);
    // solrQuery.setTermsLimit(10);
    // solrQuery.setTermsMinCount(1);
    // solrQuery.setTermsPrefix(query.getPrefix());
    // solrQuery.setTermsRegexFlag(query.getField());
    // solrQuery.setTermsSortString(query.getSortFiled());
    return solrQuery;
}

From source file:com.zb.app.external.lucene.solr.utils.BaseSolrQueryConvert.java

License:Open Source License

protected static SolrQuery createSearchQuery(List<String> params, List<String> fiters,
        SearchQuery searchQuery) {//from   ww  w .  ja v a2s  . c o  m
    SolrQuery solrQuery = new SolrQuery();
    String query = null;
    if (Argument.isEmpty(params)) {
        query = ("*:*");
    } else {
        query = StringUtils.join(params, " AND ");
        if (!StringUtils.join(params.toArray()).contains("*")) {
            solrQuery.setRequestHandler("/browse");
        }
    }
    if (Argument.isNotEmpty(fiters)) {
        solrQuery.setFilterQueries(StringUtils.join(fiters, " AND "));
    }
    solrQuery.setQuery(query);
    solrQuery.setStart(searchQuery.getStart());
    solrQuery.setRows(searchQuery.getRows());
    if (StringUtils.isNotBlank(searchQuery.getSortFiled())) {
        solrQuery.addSort(searchQuery.getSortFiled(), searchQuery.getOrderBy());
    }
    return solrQuery;
}

From source file:cz.brmlab.yodaqa.provider.solr.Solr.java

License:Apache License

public SolrDocumentList runQuery(String q, int results) throws SolrServerException {
    SolrQuery query = new SolrQuery();
    query.setQuery(escapeQuery(q));/*  ww  w  .jav a 2s  .co  m*/
    query.setRows(results);
    query.setFields("*", "score");
    QueryResponse rsp;
    while (true) {
        try {
            rsp = server.query(query, METHOD.POST);
            break; // Success!
        } catch (SolrServerException e) {
            if (e.getRootCause() instanceof IOException)
                notifyRetry(e);
            else
                throw e;
        }
    }
    return rsp.getResults();
}

From source file:cz.brmlab.yodaqa.provider.solr.Solr.java

License:Apache License

public String getDocText(String id) throws SolrServerException {
    String q = "id:" + id;
    SolrQuery query = new SolrQuery();
    query.setQuery(q);/*ww w.j a v  a2 s  .c om*/
    query.setFields("text");
    QueryResponse rsp;
    while (true) {
        try {
            rsp = server.query(query);
            break; // Success!
        } catch (SolrServerException e) {
            if (e.getRootCause() instanceof IOException)
                notifyRetry(e);
            else
                throw e;
        }
    }

    String docText = "";
    if (rsp.getResults().getNumFound() > 0) {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        ArrayList<String> results = (ArrayList) rsp.getResults().get(0).getFieldValues("text");
        docText = results.get(0);
    }
    return docText;
}

From source file:cz.incad.kramerius.k5indexer.Server.java

public SolrDocumentList getDocs(String query) throws SolrServerException {
    SolrQuery squery = new SolrQuery();
    squery.setQuery(query);//from  ww  w. ja  va2  s  . c o m
    squery.addSort("price", SolrQuery.ORDER.asc);
    QueryResponse rsp = server.query(squery);
    return rsp.getResults();
}

From source file:cz.incad.vdk.client.tools.Search.java

License:Open Source License

public JSONArray getSuggest() {
    try {//from ww  w  . jav a  2 s .  c o  m
        String q = req.getParameter("term");
        SolrQuery query = new SolrQuery();
        if (q == null || q.equals("")) {
            return new JSONArray();
        }

        query.setParam(CommonParams.QT, "/terms");
        query.setTerms(true);
        query.setTermsPrefix(q.toUpperCase());
        query.setTermsLowerInclusive(true);
        query.addTermsField("title_suggest");
        JSONArray ja = new JSONObject(IndexerQuery.terms(query)).getJSONObject("terms")
                .getJSONArray("title_suggest");
        JSONArray ret = new JSONArray();
        for (int i = 0; i < ja.length(); i = i + 2) {
            String val = ja.getString(i);
            ret.put(new JSONObject().put("value", val).put("label", val.substring(val.indexOf("##") + 2)));
        }

        return ret;
    } catch (IOException ex) {
        Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        return new JSONArray();
    }
}

From source file:cz.incad.vdk.client.tools.Search.java

License:Open Source License

public String getAsXML() throws JSONException {

    try {// w  w w.j a  v a2  s . c o m

        String q = req.getParameter("q");
        SolrQuery query = new SolrQuery();
        if (q == null || q.equals("")) {
            q = "*:*";
            query.setSort("_version_", SolrQuery.ORDER.desc);
        }
        query.setQuery(q);
        query.set("q.op", "AND");
        query.setFacet(true);
        query.setStart(getStart());
        query.setRows(getRows());

        if (LoggedController.isLogged(req)) {
            query.addFacetField(opts.getStrings("user_facets"));
        }
        query.addFacetField(opts.getStrings("facets"));

        query.setFacetMinCount(1);

        JSONObject others = opts.getJSONObject("otherParams");
        Iterator keys = others.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object val = others.get(key);
            if (val instanceof Integer) {
                query.set(key, (Integer) val);
            } else if (val instanceof String) {
                query.set(key, (String) val);
            } else if (val instanceof Boolean) {
                query.set(key, (Boolean) val);
            }

        }
        addFilters(query);

        return IndexerQuery.xml(query);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.indexing.AutocompleteIndexer.java

License:Apache License

/**
 * Transforms the searched phrase to a Solr document.
 * @param phrase The input searched phrase.
 * @return The Solr document representing the searched phrase.
 * @throws IllegalAccessException//from w w w. ja  v a 2 s.c o m
 * @throws IOException
 * @throws SolrServerException
 */
@Override
public SolrInputDocument prepareForIndexing(String phrase)
        throws IllegalAccessException, IOException, SolrServerException {

    SolrInputDocument document = new SolrInputDocument();

    SolrQuery query = new SolrQuery();
    query.setFields(IndexField.AUTOCOMPLETE.getValue());

    query.setQuery("uuid:autocomplete#" + phrase);
    QueryResponse response = solrServer.query(query);

    int count;
    // the phrase is not indexed yet
    if (response.getResults().size() == 0) {
        count = 0;
    }
    // already indexed
    else {
        String foundPhrase = ((List<String>) response.getResults().get(0)
                .getFieldValue(IndexField.AUTOCOMPLETE.getValue())).get(0);
        int delimiterPosition = foundPhrase.lastIndexOf('#');
        // the phrase was originally retrieved by indexing a document
        if (delimiterPosition == -1) {
            count = 0;
        }
        // the phrase was indexed after performing a search query
        else {
            count = Integer
                    .valueOf(foundPhrase.substring(foundPhrase.lastIndexOf('#') + 1, foundPhrase.length()));
        }
        count++;
    }

    document.addField(IndexField.UUID.getValue(), "autocomplete#" + phrase);
    document.addField(IndexField.ID.getValue(), 0);
    document.addField(IndexField.AUTOCOMPLETE.getValue(), phrase + "#" + count);

    return document;
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Configures the query to be processed.
 * @param inputQuery The query string./*from ww w.j  a  va  2 s.c  o  m*/
 * @param start Index of the first returned result.
 * @param count Number of results we wish to display.
 * @return Configured query.
 */
private SolrQuery configureQuery(String inputQuery, ResultCategory category, int start, int count) {
    SolrQuery query = new SolrQuery();
    query.setQuery(inputQuery);
    if (category != null && !category.equals(ResultCategory.ALL)) {
        query.addFilterQuery(IndexField.CLASS.getValue() + ":\"" + category.getValue() + "\"");
    }
    query.setStart(start);
    query.setRows(count);
    query.setHighlightSimplePre(FullTextSearchUtils.HIGHLIGHTED_TEXT_BEGIN);
    query.setHighlightSimplePost(FullTextSearchUtils.HIGHLIGHTED_TEXT_END);
    return query;
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Gets values to autocomplete for the input string.
 * The autocomplete feature works for multivalued fields and is based on a highlighting trick.
 * See http://solr.pl/en/2013/02/25/autocomplete-on-multivalued-fields-using-highlighting/
 * @param keywordStart/*from w w w  .  ja  v a2  s  .c  om*/
 * @return Autocomplete values.
 * @throws SolrServerException
 */
public Set<String> getTextToAutocomplete(String keywordStart) throws SolrServerException {
    SolrQuery query = new SolrQuery();
    query.setQuery(IndexField.AUTOCOMPLETE.getValue() + ":" + keywordStart);
    query.setFields(IndexField.AUTOCOMPLETE.getValue());
    query.setHighlight(true);
    query.setParam("hl.fl", IndexField.AUTOCOMPLETE.getValue());
    query.setHighlightSimplePre("");
    query.setHighlightSimplePost("");
    query.setRows(FullTextSearchUtils.AUTOCOMPLETE_ROWS);

    Map<String, Integer> map = new TreeMap<String, Integer>();
    QueryResponse response = solrServer.query(query);

    Set<String> foundIds = response.getHighlighting().keySet();
    for (String id : foundIds) {

        List<String> resultsPerDocument = response.getHighlighting().get(id)
                .get(IndexField.AUTOCOMPLETE.getValue());
        if (resultsPerDocument != null) {
            for (String result : resultsPerDocument) {
                String resultValue;
                int resultFrequency;
                int delimiterPosition = result.lastIndexOf('#');
                if (delimiterPosition == -1) { // autocomplete phrase was copied from title
                    resultValue = result;
                    resultFrequency = 0;
                } else {
                    resultValue = result.substring(0, delimiterPosition);
                    try {
                        resultFrequency = Integer
                                .valueOf(result.substring(delimiterPosition + 1, result.length()));
                    } catch (NumberFormatException e) {
                        resultFrequency = 0;
                    }
                }

                map.put(resultValue.toLowerCase(), resultFrequency);
            }
        }

        if (map.size() == FullTextSearchUtils.AUTOCOMPLETE_ROWS) {
            break;
        }
    }

    map = sortByValue(map);
    return map.keySet();
}