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.doculibre.constellio.services.AutocompleteServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
static private NamedList<Object> suggest(String q, String fieldName, SolrServer server, Boolean isStringField) {
    NamedList<Object> returnList = new NamedList<Object>();

    // escape special characters
    SolrQuery query = new SolrQuery();

    /*// ww w.  ja  va2s.c  om
     * // * Set terms.lower to the input term
     * query.setParam(TermsParams.TERMS_LOWER, q); // * Set terms.prefix to
     * the input term query.setParam(TermsParams.TERMS_PREFIX, q); // * Set
     * terms.lower.incl to false
     * query.setParam(TermsParams.TERMS_LOWER_INCLUSIVE, "false"); // * Set
     * terms.fl to the name of the source field
     * query.setParam(TermsParams.TERMS_FIELD, fieldName);
     */

    query.setParam(TermsParams.TERMS_FIELD, fieldName);// query.addTermsField("spell");
    query.setParam(TermsParams.TERMS_LIMIT, TERMS_LIMIT);// query.setTermsLimit(MAX_TERMS);
    query.setParam(TermsParams.TERMS, "true");// query.setTerms(true);
    query.setParam(TermsParams.TERMS_LOWER, q);// query.setTermsLower(q);
    query.setParam(TermsParams.TERMS_PREFIX, q);// query.setTermsPrefix(q);
    query.setParam(TermsParams.TERMS_MINCOUNT, TERMS_MINCOUNT);

    query.setRequestHandler(SolrServices.AUTOCOMPLETE_QUERY_NAME);

    try {
        QueryResponse qr = server.query(query);
        NamedList<Object> values = qr.getResponse();
        NamedList<Object> terms = (NamedList<Object>) values.get("terms");// TermsResponse
        // resp
        // =
        // qr.getTermsResponse();
        NamedList<Object> suggestions = (NamedList<Object>) terms.get(fieldName);// items
        // =
        // resp.getTerms("spell");
        if (!isStringField) {
            q = AnalyzerUtils.analyzePhrase(q, false);
        }

        for (int i = 0; i < suggestions.size(); i++) {
            String currentSuggestion = suggestions.getName(i);
            // System.out.println(currentSuggestion);
            if (isStringField) {
                if (currentSuggestion.contains(q)) {
                    // String suffix =
                    // StringUtils.substringAfter(currentSuggestion, q);
                    // if (suffix.isEmpty() &&
                    // !currentSuggestion.equals(q)){
                    // //q n est pas dans currentSuggestion
                    // break;
                    // }
                    if (currentSuggestion.contains(SPECIAL_CHAR)) {
                        // le resultat de recherche retourne des fois une
                        // partie de la valeur existant
                        // dans le champ!
                        currentSuggestion = StringUtils.substringAfter(currentSuggestion, SPECIAL_CHAR);
                    }
                    returnList.add(currentSuggestion, suggestions.getVal(i));
                }
            } else {
                currentSuggestion = AnalyzerUtils.analyzePhrase(currentSuggestion, false);
                if (currentSuggestion.contains(q)) {
                    returnList.add(currentSuggestion, suggestions.getVal(i));
                }
            }
        }

    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }
    return returnList;
}

From source file:com.doculibre.constellio.services.IndexFieldServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  ww . j  av a 2 s . c o m*/
public List<String> suggestValues(IndexField indexField, String text) {
    List<String> values = new ArrayList<String>();
    RecordCollection collection = indexField.getRecordCollection();
    SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
    SolrServer solrServer = solrServices.getSolrServer(collection);
    if (solrServer != null) {
        SolrQuery query = new SolrQuery();
        query.setRequestHandler("/admin/luke");
        query.setParam(CommonParams.FL, indexField.getName());
        query.setParam(LukeRequestHandler.NUMTERMS, "" + 100);

        if (text != null) {
            query.setQuery(indexField.getName() + ":" + text + "*");
        }
        if (collection.isOpenSearch()) {
            query.setParam("openSearchURL", collection.getOpenSearchURL());
        }

        try {
            QueryResponse queryResponse = solrServer.query(query);
            NamedList<Object> fields = (NamedList<Object>) queryResponse.getResponse().get("fields");
            if (fields != null) {
                NamedList<Object> field = (NamedList<Object>) fields.get(indexField.getName());
                if (field != null) {
                    NamedList<Object> topTerms = (NamedList<Object>) field.get("topTerms");
                    if (topTerms != null) {
                        for (Map.Entry<String, Object> topTerm : topTerms) {
                            String topTermKey = topTerm.getKey();
                            if (text == null || topTermKey.toLowerCase().startsWith(text.toLowerCase())) {
                                values.add(topTerm.getKey());
                            }
                        }
                    }
                }
            }
        } catch (SolrServerException e) {
            throw new RuntimeException(e);
        }
    }
    return values;
}

From source file:com.doculibre.constellio.services.SearchServicesImpl.java

License:Open Source License

public static SolrQuery toSolrQuery(SimpleSearch simpleSearch, boolean useDismax, boolean withMultiValuedFacets,
        boolean withSingleValuedFacets, boolean notIncludedOnly) {
    SolrQuery query = new SolrQuery();

    boolean addSynonyms = !SolrServices.synonymsFilterActivated;
    if (addSynonyms) {
        addQueryTextAndOperatorWithSynonyms(simpleSearch, query, useDismax);
    } else {/*from  ww  w  . j av a2  s.c om*/
        addQueryTextAndOperatorWithoutSynonyms(simpleSearch, query, useDismax);
    }

    // FIXME confirmer avec Vincent:
    // 1. que les tags sont vraiment a ajouter par defaut (meme pour openSearch)
    // 2. separer les tags par des AND et non des OU
    addTagsTo(simpleSearch, query);

    boolean addFacets = withMultiValuedFacets || withSingleValuedFacets;
    if (addFacets) {
        addFacetsTo(simpleSearch, query, withMultiValuedFacets, withSingleValuedFacets, notIncludedOnly);
    }

    return query;
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

@Override
public SolrDocument get(String docId, RecordCollection collection) {
    SolrDocument doc;/*from  www .  j  av a  2s .  c om*/
    SolrServer solrServer = getSolrServer(collection);
    SolrQuery query = new SolrQuery();
    String escapedDocId = ClientUtils.escapeQueryChars(docId);
    query.setQuery(IndexField.UNIQUE_KEY_FIELD + ":" + escapedDocId + "");
    try {
        QueryResponse queryResponse = solrServer.query(query);
        SolrDocumentList solrDocumentList = queryResponse.getResults();
        if (!solrDocumentList.isEmpty()) {
            doc = solrDocumentList.get(0);
        } else {
            doc = null;
        }
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }
    return doc;
}

From source file:com.doculibre.constellio.services.StatusServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   www. j av a  2  s.  c  o m*/
public int countIndexedRecords(RecordCollection collection) {
    int count;
    SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
    SolrServer solrServer = solrServices.getSolrServer(collection);
    if (solrServer != null) {
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setStart(0);
        query.setRows(0);
        try {
            QueryResponse queryResponse = solrServer.query(query);
            count = (int) queryResponse.getResults().getNumFound();
        } catch (SolrServerException e) {
            throw new RuntimeException(e);
        }
    } else {
        count = 0;
    }
    return count;
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Do the query using a SolrQuery//from ww  w .j  a v a 2  s  . c o m
 */
public static QueryResponse doThirdQuery(SolrServer server) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(query);
    solrQuery.set("collectionName", myCollection);
    solrQuery.set("facet", facet);
    solrQuery.setStart(start);
    solrQuery.setRows(nbDocuments);
    return server.query(solrQuery);
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Do the query using a SolrQuery//from   w  w  w. jav  a 2  s  . c  o  m
 */
public static QueryResponse spellCheck(SolrServer server, String badQuery) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(badQuery);
    solrQuery.set("collectionName", myCollection);

    // qt=spellcheck || qt=spellchecker
    solrQuery.setRequestHandler("spellcheck");
    return server.query(solrQuery);
}

From source file:com.doculibre.constellio.stats.StatsCompiler.java

License:Open Source License

public synchronized void saveStats(SimpleSearch simpleSearch, SolrServer indexJournal, SolrServer indexCompile,
        QueryResponse res) throws SolrServerException, IOException {
    String collectionName = simpleSearch.getCollectionName();
    String luceneQuery = simpleSearch.getLuceneQuery();

    GregorianCalendar calendar = new GregorianCalendar();
    Date time = new Date();
    calendar.setTime(time);/* w  ww .  j a  v a 2 s  . c  o m*/
    String query = luceneQuery;
    String escapedQuery = escape(query);
    long nbRes = res.getResults().getNumFound();
    long qTime = res.getQTime();
    String desplayDate = time.toString();
    String searchDate = format(time);
    String queryWithParams = simpleSearch.toSimpleParams().toString();

    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", desplayDate + query);
    doc.addField("query", query);
    doc.addField("queryWithParams", queryWithParams);
    doc.addField("nbres", "" + nbRes);
    doc.addField("qtime", "" + qTime);
    doc.addField("dateaffiche", desplayDate);
    doc.addField("date", searchDate);
    doc.addField("recherche", "recherche");
    doc.addField("collection", collectionName);

    UpdateRequest up = new UpdateRequest();
    up.setAction(ACTION.COMMIT, true, true);
    up.add(doc);

    up.process(indexJournal);

    String compileId = "collection_" + collectionName + " id_" + escapedQuery;
    SolrQuery solrQuery = new SolrQuery();
    // Requte Lucene
    solrQuery.setQuery("id:\"" + compileId + "\"");
    // nb rsultats par page
    solrQuery.setRows(15);
    // page de dbut
    solrQuery.setStart(0);
    QueryResponse qr = indexCompile.query(solrQuery);
    if (qr.getResults().getNumFound() > 0) {
        SolrDocument sd = qr.getResults().get(0);
        long freq = (Long) sd.getFieldValue("freq");
        long click = (Long) sd.getFieldValue("click");
        // indexCompile.deleteById(query);
        SolrInputDocument docCompile = new SolrInputDocument();
        docCompile.addField("id", compileId);
        docCompile.addField("query", query);
        if (!((String) sd.getFieldValue("nbres")).equals("0")) {
            ConstellioSpringUtils.getAutocompleteServices().onQueryAdd(docCompile, query);
        }
        docCompile.addField("freq", freq + 1);
        docCompile.addField("nbres", "" + nbRes);
        docCompile.addField("recherche", "recherche");
        docCompile.addField("collection", collectionName);
        if (nbRes == 0) {
            docCompile.addField("zero", "true");
        } else {
            docCompile.addField("zero", "false");
        }
        docCompile.addField("click", click);
        if (click == 0) {
            docCompile.addField("clickstr", "zero");
        } else {
            docCompile.addField("clickstr", "notzero");
        }
        up.clear();
        up.setAction(ACTION.COMMIT, true, true);
        up.add(docCompile);

        up.process(indexCompile);
    } else {
        SolrInputDocument docCompile = new SolrInputDocument();
        docCompile.addField("id", compileId);
        docCompile.addField("query", query);
        if (nbRes != 0) {
            ConstellioSpringUtils.getAutocompleteServices().onQueryAdd(docCompile, query);
        }
        docCompile.addField("freq", 1);
        docCompile.addField("recherche", "recherche");
        docCompile.addField("collection", collectionName);
        docCompile.addField("nbres", "" + nbRes);
        if (nbRes == 0) {
            docCompile.addField("zero", "true");
        } else {
            docCompile.addField("zero", "false");
        }
        docCompile.addField("click", 0);
        docCompile.addField("clickstr", "zero");
        up.clear();
        up.setAction(ACTION.COMMIT, true, true);
        up.add(docCompile);

        up.process(indexCompile);
    }
}

From source file:com.doculibre.constellio.stats.StatsCompiler.java

License:Open Source License

public synchronized void computeClick(String collectionName, SolrServer indexCompile, SimpleSearch simpleSearch)
        throws SolrServerException, IOException {
    String query = simpleSearch.getLuceneQuery();
    String escapedQuery = escape(query);
    String compileId = "collection_" + collectionName + " id_" + escapedQuery;

    SolrQuery solrQuery = new SolrQuery();
    // Requte Lucene
    solrQuery.setQuery("id:\"" + compileId + "\"");
    // nb rsultats par page
    solrQuery.setRows(15);/*from ww w.  j a v a  2 s.com*/
    // page de dbut
    solrQuery.setStart(0);

    QueryResponse qr = indexCompile.query(solrQuery);
    if (qr.getResults().getNumFound() > 0) {
        SolrDocument sd = qr.getResults().get(0);
        long click = (Long) sd.getFieldValue("click");
        indexCompile.deleteById(escapedQuery);
        SolrInputDocument docCompile = new SolrInputDocument();
        docCompile.addField("id", compileId);
        docCompile.addField("query", query);
        if (!((String) sd.getFieldValue("nbres")).equals("0")) {
            ConstellioSpringUtils.getAutocompleteServices().onQueryAdd(docCompile, query);
        }
        docCompile.addField("freq", (Long) sd.getFieldValue("freq"));
        docCompile.addField("recherche", "recherche");
        docCompile.addField("zero", (String) sd.getFieldValue("zero"));
        docCompile.addField("nbres", (String) sd.getFieldValue("nbres"));
        docCompile.addField("click", (click + 1));
        docCompile.addField("clickstr", "notzero");
        docCompile.addField("collection", collectionName);
        UpdateRequest up = new UpdateRequest();
        up.setAction(ACTION.COMMIT, true, true);
        up.add(docCompile);
        up.process(indexCompile);
    }
}

From source file:com.doculibre.constellio.stats.StatsCompiler.java

License:Open Source License

public synchronized void computeClickUrl(String collectionName, String url, String recordURL,
        SolrServer indexurl, SimpleSearch simpleSearch) throws SolrServerException, IOException {
    String query = simpleSearch.getLuceneQuery();
    String escapedQuery = escape(query);
    String compileId = "url_" + url + " collection_" + collectionName + " id_" + escapedQuery;
    SolrQuery solrQuery = new SolrQuery();
    // Requte Lucene
    solrQuery.setQuery("id:\"" + compileId + "\"");
    // nb rsultats par page
    solrQuery.setRows(15);//from  w  ww.  j  av a  2 s.  c  o  m
    // page de dbut
    solrQuery.setStart(0);
    QueryResponse qr = indexurl.query(solrQuery);
    if (qr.getResults().getNumFound() > 0) {
        SolrDocument sd = qr.getResults().get(0);
        long nbClick = (Long) sd.getFieldValue("nbclick");
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", compileId);
        doc.addField("query", escapedQuery);
        doc.addField("url", url);
        doc.addField("nbclick", nbClick + 1);
        doc.addField("recordURL", recordURL);
        doc.addField("collectionName", collectionName);
        UpdateRequest up = new UpdateRequest();
        up.setAction(ACTION.COMMIT, true, true);
        up.add(doc);
        up.process(indexurl);
    } else {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", compileId);
        doc.addField("query", escapedQuery);
        doc.addField("url", url);
        doc.addField("nbclick", 0);
        doc.addField("recordURL", recordURL);
        doc.addField("collectionName", collectionName);
        UpdateRequest up = new UpdateRequest();
        up.setAction(ACTION.COMMIT, true, true);
        up.add(doc);
        up.process(indexurl);
    }
}