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.frank.search.solr.core.DefaultQueryParser.java

License:Apache License

/**
 * Convert given Query into a SolrQuery executable via
 * {@link org.apache.solr.client.solrj.SolrClient}
 *
 * @param query//from  w w  w  . j ava2s. co m
 * @return
 */
@Override
public final SolrQuery doConstructSolrQuery(SolrDataQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");
    Assert.notNull(query.getCriteria(), "Query has to have a criteria.");

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.Q, getQueryString(query));
    if (query instanceof Query) {
        processQueryOptions(solrQuery, (Query) query);
    }
    if (query instanceof FacetQuery) {
        processFacetOptions(solrQuery, (FacetQuery) query);
    }
    if (query instanceof HighlightQuery) {
        processHighlightOptions(solrQuery, (HighlightQuery) query);
    }
    return solrQuery;
}

From source file:com.frank.search.solr.core.TermsQueryParser.java

License:Apache License

@Override
public SolrQuery doConstructSolrQuery(TermsQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");

    SolrQuery solrQuery = new SolrQuery();
    String queryString = getQueryString(query);
    if (StringUtils.hasText(queryString)) {
        solrQuery.setParam(CommonParams.Q, queryString);
    }/*from  ww w . ja  va  2s.  c o m*/
    appendTermsOptionsToSolrQuery(query.getTermsOptions(), solrQuery);
    processTermsFields(solrQuery, query);
    appendRequestHandler(solrQuery, query.getRequestHandler());
    return solrQuery;
}

From source file:com.gdn.x.ui.controller.CommonController.java

protected List searchByQuery(String queryParameter) throws MalformedURLException, SolrServerException {
    List actualResult = new ArrayList<String>();
    try {//from w w w .  j  av a2 s.  com
        //            for solr 5
        HttpSolrClient solr = new HttpSolrClient(solrUrlSearch);
        //for Solr 4
        //            HttpSolrServer solr = new HttpSolrServer("");

        SolrQuery query = new SolrQuery();

        query.setQuery(queryParameter);
        query.setFields("id");
        query.setRequestHandler("/browse");
        query.setStart(0);
        query.setRows(40);
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        int j = 0;
        for (int i = 0; i < results.size(); ++i, j++) {
            actualResult.add(results.get(i).get("id"));
        }
    } catch (IOException ex) {
        Logger.getLogger(RunGoldenListController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return actualResult;
}

From source file:com.gdn.x.ui.controller.Evaluation.CommonControllerEvaluation.java

protected List searchByQueryEvaluation(String queryParameter)
        throws MalformedURLException, SolrServerException {
    List actualResult = new ArrayList<String>();
    try {/* w  w  w  . j a v  a2s  .  com*/
        //            for solr 5
        HttpSolrClient solr = new HttpSolrClient(solrUrlSearch);
        //for Solr 4
        //            HttpSolrServer solr = new HttpSolrServer("http://172.17.132.9:8983/solr/collection3");

        SolrQuery query = new SolrQuery();

        query.setQuery(queryParameter);
        query.setFields("id");
        query.setRequestHandler("/browse");
        query.setStart(0);
        query.setRows(24);
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        int j = 0;
        for (int i = 0; i < results.size(); ++i, j++) {
            actualResult.add(results.get(i).get("id"));
        }
        //                System.out.println(results.getNumFound());
        //            System.out.println(actualResult);

    } catch (IOException ex) {
        Logger.getLogger(RunGoldenListController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return actualResult;
}

From source file:com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.SpellCheckerIndexer.java

License:Open Source License

public boolean buildIndex(SpellCheckerDictionaryNames spellCheckerDictionaryName) {
    if (!SpellCheckerConfig.isEnabled()) {
        return false;
    }/* w  w  w .j a v  a2s. co m*/

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType(Constants.SolrQueryType.spellcheck.toString());
    solrQuery.add(Constants.SPELLCHECKER_DICTIONARY_NAME_PARAMETER, spellCheckerDictionaryName.toString());
    solrQuery.add(Constants.SPELLCHECKER_BUILD_PARAMETER, "true");
    solrQuery.add(Constants.SPELLCHECKER_ENABLED_PARAMETER, "true");
    solrQuery.setQuery("spell");
    try {
        QueryResponse response = solrClient.getServer().query(solrQuery);
        if (response.getStatus() != 0) {
            logger.error("Indexing dictionary " + spellCheckerDictionaryName.name() + " fails");
            return false;
        }
        logger.info("Successfully indexing dictionary " + spellCheckerDictionaryName.name());
        return true;

    } catch (Exception e) {
        logger.error(
                "An error has occured when indexing spellchecker " + spellCheckerDictionaryName + " : " + e);
        return false;
    }

}

From source file:com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.SpellCheckerIndexerTest.java

License:Open Source License

@Test
public void testThatSpellCheckerShouldNotAcceptAnInexistingSpellCheckerDictionaryName() {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType(Constants.SolrQueryType.spellcheck.toString());
    solrQuery.add(Constants.SPELLCHECKER_DICTIONARY_NAME_PARAMETER, "notExistingInSolrConfig.xml");
    solrQuery.add(Constants.SPELLCHECKER_BUILD_PARAMETER, "true");
    solrQuery.add(Constants.SPELLCHECKER_ENABLED_PARAMETER, "true");
    solrQuery.setQuery("spell");
    try {/*  w ww.j av a  2 s.com*/
        QueryResponse response = solrClient.getServer().query(solrQuery);
        if (response.getStatus() != 0) {
            fail("Status should not be 0 when the name of the dictionnary name is not defined in solrConfig.xml");
        }
        fail("dictionnary name that are not defined in solrConfig.xml should not be accepted");
    } catch (Exception e) {
        logger.error(e);
    }

}

From source file:com.gisgraphy.domain.repository.GenericGisDao.java

License:Open Source License

/**
 * Do a full text search for the given name. The search will be case,
 * iso-latin, comma-separated insensitive<br>
 * search for 'saint-Andr', 'saint-Andre', 'SaInT-andr', 'st-andr', etc
 * will return the same results. <u>note</u> : search for zipcode too
 * Polymorphism is not supported, e.g : if clazz=GisFeature : the results
 * will only be of that type and no other feature type (e.g : City that
 * extends gisFeature...etc) will be returned. The results will be sort by
 * relevance./*w  w  w  . ja v a 2 s . com*/
 * 
 * @param name
 *                The name to search for
 * @param includeAlternateNames
 *                Wether we search in the alternatenames too
 * @param clazz
 *                specify the features we want to search for, if null : no
 *                restriction is apply
 * @return a list of gisFeatures of type of the class for the given text.
 *         the max list size is {@link GenericGisDao#MAX_FULLTEXT_RESULTS};
 * @see IGisFeatureDao#listAllFeaturesFromText(String, boolean)
 */
protected List<T> listFromText(String name, boolean includeAlternateNames, Class<T> clazz) {
    logger.debug("getFromText " + name);
    // Set up a simple query
    // Check for a null or empty string query
    if (name == null || name.length() == 0) {
        return new ArrayList<T>();
    }

    SolrQuery query = new SolrQuery();
    String namefield = FullTextFields.ALL_NAME.getValue();
    if (!includeAlternateNames) {
        namefield = FullTextFields.NAME.getValue();
    }
    String queryString = "(" + namefield + ":\"" + name + "\" OR " + FullTextFields.ZIPCODE.getValue() + ":\""
            + name + "\")";
    if (clazz != null) {
        queryString += " AND placetype:" + persistentClass.getSimpleName();
    }
    query.setQuery(queryString);
    query.setQueryType(Constants.SolrQueryType.advanced.toString());
    query.setFields(FullTextFields.FEATUREID.getValue());
    query.setRows(MAX_FULLTEXT_RESULTS);

    QueryResponse results = null;
    try {
        results = solrClient.getServer().query(query);
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

    List<Long> ids = new ArrayList<Long>();
    for (SolrDocument doc : results.getResults()) {
        ids.add((Long) doc.getFieldValue(FullTextFields.FEATUREID.getValue()));
    }
    // log
    List<T> gisFeatureList = this.listByFeatureIds(ids);
    if (logger.isDebugEnabled()) {
        logger.debug("search on " + name + " returns " + gisFeatureList.size());
        for (GisFeature gisFeature : gisFeatureList) {
            logger.debug("search on " + name + " returns " + gisFeature.getName());
        }
    }

    return gisFeatureList;
}

From source file:com.gisgraphy.domain.repository.SolRSynchroniserTest.java

License:Open Source License

private QueryResponse searchInFulltextSearchEngine(String searchWords) {
    SolrQuery query = new SolrQuery();
    String namefield = FullTextFields.ALL_NAME.getValue();

    String queryString = "(" + namefield + ":\"" + searchWords + "\")";
    query.setQuery(queryString);/*ww w .  j  a  v a 2s .  c  om*/
    query.setQueryType(Constants.SolrQueryType.advanced.toString());
    query.setFields(FullTextFields.FEATUREID.getValue());

    QueryResponse resultsAfterRemove = null;
    try {
        resultsAfterRemove = solrClient.getServer().query(query);
    } catch (SolrServerException e) {
        fail();
    }
    return resultsAfterRemove;
}

From source file:com.googlecode.solrgeonames.server.GeoServlet.java

License:Open Source License

/**
 * Prepare a 'suggest' query response//from  w w  w. j  av  a2s  .c o  m
 *
 * @param request The incoming request
 * @param response The response object
 * @throws IOException If errors found
 */
private QueryResponse runQuery(String query, int start, int rows, String fields, String filter, String[] facets,
        int facetLimit) throws Exception {
    SolrQuery q = new SolrQuery();
    q.setQuery(query);
    q.setStart(start);
    q.setRows(rows);
    q.setFields(fields);
    if (filter != null) {
        q.setFilterQueries(filter);
    }
    if (facets != null) {
        q.setFacet(true);
        q.setFacetLimit(facetLimit);
        q.addFacetField(facets);
    } else {
        q.setFacet(false);
    }
    return solrServer.query(q);
}

From source file:com.hurence.logisland.service.solr.api.SolrClientService.java

License:Apache License

@Override
public void copyCollection(String reindexScrollTimeout, String src, String dst)
        throws DatastoreClientServiceException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setRows(1000);/* w ww . j  a v  a2s .co m*/
    solrQuery.setQuery("*:*");
    solrQuery.addSort("id", SolrQuery.ORDER.asc); // Pay attention to this line
    String cursorMark = CursorMarkParams.CURSOR_MARK_START;
    boolean done = false;
    QueryResponse response;
    try {
        do {
            solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
            response = getClient().query(src, solrQuery);
            List<SolrInputDocument> documents = new ArrayList<>();
            for (SolrDocument document : response.getResults()) {
                SolrInputDocument inputDocument = getConverter().toSolrInputDocument(document);
                inputDocument.removeField("_version_");
                documents.add(inputDocument);
            }

            getClient().add(dst, documents);

        } while (cursorMark.equals(response.getNextCursorMark()));

        getClient().commit(dst);
    } catch (Exception e) {
        throw new DatastoreClientServiceException(e);
    }
}