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

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

Introduction

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

Prototype

public SolrQuery setQuery(String query) 

Source Link

Usage

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

License:Open Source License

private static void addQueryTextAndOperatorWithSynonyms(SimpleSearch simpleSearch, SolrQuery query,
        boolean useDismax) {
    String collectionName = simpleSearch.getCollectionName();
    if (StringUtils.isNotEmpty(collectionName)) {
        if (simpleSearch.getAdvancedSearchRule() == null) {
            String textQuery = getTextQuery(simpleSearch);
            if (StringUtils.isNotEmpty(textQuery)) {
                String searchType = simpleSearch.getSearchType();
                StringBuffer sb = new StringBuffer();
                // sb.append("(");
                if (SimpleSearch.SEARCH_ALL.equals(textQuery)) {
                    sb.append(textQuery);
                    if (useDismax) {
                        // Non valide avec disMax => disMax doit etre desactivee
                        query.setRequestHandler(SolrServices.DEFAULT_DISTANCE_NAME);
                        LOGGER.warning(/*from   www .  ja  va 2s.  c  o  m*/
                                "Dismax is replaced by the default distance since the former does not allow wildcard");
                    }
                } else if (SimpleSearch.EXACT_EXPRESSION.equals(searchType)) {
                    // FIXME a corriger : si "n" terms avec chacun "m" synonyms => traiter les combinaison
                    // de
                    // synonymes
                    // Sinon solution simple: synonymes de l'expression (solution prise pour l'instant)
                    String textAndSynonyms = SynonymUtils.addSynonyms(textQuery, collectionName, true);
                    sb.append(textAndSynonyms);// SynonymUtils.addSynonyms(textQuery,
                    // collectionName)
                } else {
                    // TOUS_LES_MOTS OU AU_MOINS_UN_MOT
                    String operator;
                    if (SimpleSearch.AT_LEAST_ONE_WORD.equals(searchType)) {
                        operator = "OR";
                    } else {
                        operator = "AND";
                    }
                    String[] terms = textQuery.split(" ");
                    for (int i = 0; i < terms.length; i++) {
                        String term = terms[i];
                        String termAndSynonyms = SynonymUtils.addSynonyms(term, collectionName, false);
                        if (term.equals(termAndSynonyms)) {
                            sb.append(term);
                        } else {
                            sb.append("(" + termAndSynonyms + ")");
                        }
                        if (i < terms.length - 1) {
                            // sb.append(operator);
                            sb.append(" " + operator + " ");
                        }
                    }
                }
                // sb.append(")");
                query.setQuery(sb.toString());
            }
        } else {
            query.setQuery(simpleSearch.getLuceneQuery());
        }
    }
}

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

License:Open Source License

private static void addQueryTextAndOperatorWithoutSynonyms(SimpleSearch simpleSearch, SolrQuery query,
        boolean useDismax) {

    String collectionName = simpleSearch.getCollectionName();
    if (StringUtils.isNotEmpty(collectionName)) {
        if (simpleSearch.getAdvancedSearchRule() == null) {
            String textQuery = getTextQuery(simpleSearch);
            if (StringUtils.isNotEmpty(textQuery)) {
                String searchType = simpleSearch.getSearchType();
                if (SimpleSearch.SEARCH_ALL.equals(textQuery)) {
                    query.setQuery(textQuery);
                    // FIXME : AND ou Operateur par defaut?
                    query.setParam("q.op", "AND");
                    if (useDismax) {
                        // Non valide avec disMax => disMax doit etre desactivee
                        query.setRequestHandler(SolrServices.DEFAULT_DISTANCE_NAME);
                        LOGGER.warning(/*w w w. j  a va 2  s .  com*/
                                "Dismax is replaced by the default distance since the former does not allow wildcard");
                    }
                } else if (SimpleSearch.AT_LEAST_ONE_WORD.equals(searchType)) {
                    query.setQuery(textQuery);
                    // Operateur OR
                    query.setParam("q.op", "OR");
                    if (useDismax) {
                        query.setParam("mm", "0");
                    }
                } else if (SimpleSearch.EXACT_EXPRESSION.equals(searchType)) {
                    query.setQuery("\"" + textQuery + "\"");
                    if (useDismax) {
                        // FIXME il faut faire quoi avec dismax?
                    }
                } else {
                    if (SimpleSearch.ALL_WORDS.equals(searchType)) {
                        query.setQuery(textQuery);
                        // Operateur AND
                        query.setParam("q.op", "AND");
                        if (useDismax) {
                            query.setParam("mm", "100");
                        }
                    } else {
                        throw new RuntimeException("Invalid searchType " + searchType);
                    }
                }
            }
        } else {
            query.setQuery(simpleSearch.getLuceneQuery());
        }
    }
}

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

License:Open Source License

@Override
public SolrDocument get(String docId, RecordCollection collection) {
    SolrDocument doc;/*  www. j a v  a2 s . c  o  m*/
    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   ww w  .  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 .ja  va 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/*  ww w  .  j  av a 2 s .co  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);//from   w  w  w. j av a 2s.  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  w w  w .  j ava2 s  .  c o  m
    // 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  ww  w.  j  ava 2s.c om*/
    // 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);
    }
}

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

License:Open Source License

public QueryResponse getMostPopularQueries(String collectionName, SolrServer server, Date startDate,
        Date endDate, int start, int row) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setStart(start);/*from   www. j  a  v  a  2  s.  c o  m*/
    solrQuery.setRows(row);
    solrQuery.setSort("freq", SolrQuery.ORDER.desc);
    StringBuffer querySB = new StringBuffer("collection:" + collectionName + " recherche:recherche");
    //      querySB.append(" date:[");
    //      querySB.append(StatsCompiler.format(startDate));
    //      querySB.append(" TO ");
    //      querySB.append(StatsCompiler.format(endDate));
    //      querySB.append("]");
    solrQuery.setQuery(querySB.toString());
    return server.query(solrQuery);
}