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.gisgraphy.domain.geoloc.service.fulltextsearch.spell.SpellCheckerIndexer.java

License:Open Source License

public boolean buildIndex(SpellCheckerDictionaryNames spellCheckerDictionaryName) {
    if (!SpellCheckerConfig.isEnabled()) {
        return false;
    }/*from   www  .ja va 2 s. c o 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 {/* www. j  a  v a  2 s  .c o  m*/
        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. j  av  a  2s .  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);
    query.setQueryType(Constants.SolrQueryType.advanced.toString());
    query.setFields(FullTextFields.FEATUREID.getValue());

    QueryResponse resultsAfterRemove = null;
    try {//from  w w w. j a  v  a2 s.  c o m
        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 ww w.  j  av a2s . com
 *
 * @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  ava  2 s  . 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);
    }
}

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

License:Apache License

public Collection<Record> query(String queryString, String collectionName) {
    try {//from  w  w w . j  av a 2s. c  om
        String uniqueKey = getUniqueKey(collectionName);
        SolrQuery query = new SolrQuery();
        query.setQuery(queryString);

        QueryResponse response = getClient().query(collectionName, query);

        Collection<Record> results = new ArrayList<>();
        response.getResults().forEach(document -> {
            results.add(getConverter().toRecord(document, uniqueKey));
        });

    } catch (SolrServerException | IOException e) {
        logger.error(e.toString());
        throw new DatastoreClientServiceException(e);
    }

    return null;
}

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

License:Apache License

public long queryCount(String queryString, String collectionName) {
    try {//from ww  w .  j  a  v  a2s . co m
        SolrQuery query = new SolrQuery();
        query.setQuery(queryString);

        QueryResponse response = getClient().query(collectionName, query);

        return response.getResults().getNumFound();

    } catch (SolrServerException | IOException e) {
        logger.error(e.toString());
        throw new DatastoreClientServiceException(e);
    }
}

From source file:com.hurence.logisland.service.solr.Solr_6_4_2_ChronixClientService.java

License:Apache License

@Override
public Collection<Record> query(String queryString) {
    try {/*from  ww  w.jav a 2s.  c o  m*/
        SolrQuery query = new SolrQuery();
        query.setQuery(queryString);

        QueryResponse response = solr.query(query);

        //response.getResults().forEach(doc -> doc.);

    } catch (SolrServerException | IOException e) {
        logger.error(e.toString());
        throw new DatastoreClientServiceException(e);
    }

    return null;
}

From source file:com.hurence.logisland.service.solr.Solr_6_4_2_ChronixClientService.java

License:Apache License

@Override
public long queryCount(String queryString) {
    try {/*from  w ww  .  ja  va2s.  c o  m*/
        SolrQuery query = new SolrQuery();
        query.setQuery(queryString);

        QueryResponse response = solr.query(query);

        return response.getResults().getNumFound();

    } catch (SolrServerException | IOException e) {
        logger.error(e.toString());
        throw new DatastoreClientServiceException(e);
    }
}