Search in sources :

Example 81 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project YCSB by brianfrankcooper.

the class SolrClient method read.

/**
   * Read a record from the database. Each field/value pair from the result will be stored in a
   * HashMap.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to read.
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error or "not found".
   */
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    try {
        Boolean returnFields = false;
        String[] fieldList = null;
        if (fields != null) {
            returnFields = true;
            fieldList = fields.toArray(new String[fields.size()]);
        }
        SolrQuery query = new SolrQuery();
        query.setQuery("id:" + key);
        if (returnFields) {
            query.setFields(fieldList);
        }
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();
        if ((results != null) && (results.getNumFound() > 0)) {
            for (String field : results.get(0).getFieldNames()) {
                result.put(field, new StringByteIterator(String.valueOf(results.get(0).getFirstValue(field))));
            }
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) StringByteIterator(com.yahoo.ycsb.StringByteIterator) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 82 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project play-cookbook by spinscale.

the class SolrSearchTest method testCarsWithDynamicSetup.

@Test
public void testCarsWithDynamicSetup() throws Exception {
    Car c = Car.find("byBrand", "BMW").first();
    assertNotNull(c);
    SolrQuery query = new SolrQuery();
    query.setQuery("brand_s:BMW");
    QueryResponse rp = server.query(query);
    SolrDocumentList results = rp.getResults();
    assertEquals(1, results.size());
    assertEquals("BMW", results.get(0).getFieldValue("brand_s"));
    assertEquals(c.getClass().getName() + ":" + c.id.toString(), results.get(0).getFieldValue("id"));
}
Also used : Car(models.Car) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) UnitTest(play.test.UnitTest) Test(org.junit.Test)

Example 83 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project jaggery by wso2.

the class RegistryHostObject method search.

private ResourceData[] search(UserRegistry registry, String searchQuery) throws IndexerException, RegistryException {
    SolrClient client = SolrClient.getInstance();
    SolrDocumentList results = client.query(searchQuery, registry.getTenantId());
    if (log.isDebugEnabled())
        log.debug("result received " + results);
    List<ResourceData> filteredResults = new ArrayList<ResourceData>();
    for (int i = 0; i < results.getNumFound(); i++) {
        SolrDocument solrDocument = results.get(i);
        String path = getPathFromId((String) solrDocument.getFirstValue("id"));
        //if (AuthorizationUtils.authorize(path, ActionConstants.GET)){
        if ((registry.resourceExists(path)) && (isAuthorized(registry, path, ActionConstants.GET))) {
            filteredResults.add(loadResourceByPath(registry, path));
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("filtered results " + filteredResults + " for user " + registry.getUserName());
    }
    return filteredResults.toArray(new ResourceData[0]);
}
Also used : ResourceData(org.wso2.carbon.registry.common.ResourceData) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.wso2.carbon.registry.indexing.solr.SolrClient) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 84 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project Xponents by OpenSextant.

the class SolrMatcherSupport method tagTextCallSolrTagger.

/**
     * Solr call: tag input buffer, returning all candiate reference data that
     * matched during tagging.
     *
     * @param buffer text to tag
     * @param docid  id for text, only for tracking purposes
     * @param refDataMap
     *            - a map of reference data in solr, It will store caller's
     *            domain objects. e.g., rec.id =&gt; domain(rec)
     * @return solr response
     * @throws ExtractionException tagger error
     */
protected QueryResponse tagTextCallSolrTagger(String buffer, String docid, final Map<Integer, Object> refDataMap) throws ExtractionException {
    SolrTaggerRequest tagRequest = new SolrTaggerRequest(getMatcherParameters(), buffer);
    tagRequest.setPath(requestHandler);
    // Stream the response to avoid serialization and to save memory by
    // only keeping one SolrDocument materialized at a time
    tagRequest.setStreamingResponseCallback(new StreamingResponseCallback() {

        @Override
        public void streamDocListInfo(long numFound, long start, Float maxScore) {
        }

        // Future optimization: it would be nice if Solr could give us the
        // doc id without giving us a SolrDocument, allowing us to
        // conditionally get it. It would save disk IO & speed, at the
        // expense of putting ids into memory.
        @Override
        public void streamSolrDocument(final SolrDocument solrDoc) {
            Integer id = (Integer) solrDoc.getFirstValue("id");
            // create a domain object for the given tag;
            // this callback handler caches such domain obj in simple k/v
            // map.
            Object domainObj = createTag(solrDoc);
            if (domainObj != null) {
                refDataMap.put(id, domainObj);
            }
        }
    });
    QueryResponse response;
    try {
        response = tagRequest.process(solr.getInternalSolrServer());
    } catch (Exception err) {
        throw new ExtractionException("Failed to tag document=" + docid, err);
    }
    // see https://issues.apache.org/jira/browse/SOLR-5154
    SolrDocumentList docList = response.getResults();
    if (docList != null) {
        // log.debug("Not streaming docs from Solr (not supported)");
        StreamingResponseCallback callback = tagRequest.getStreamingResponseCallback();
        callback.streamDocListInfo(docList.getNumFound(), docList.getStart(), docList.getMaxScore());
        for (SolrDocument solrDoc : docList) {
            /**
                 * This appears to be an empty list; what is this explicit
                 * callback loop for?
                 */
            callback.streamSolrDocument(solrDoc);
        }
    }
    return response;
}
Also used : StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ConfigException(org.opensextant.ConfigException)

Example 85 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project Xponents by OpenSextant.

the class SolrGazetteer method loadCountries.

/**
     * This only returns Country objects that are names; It does not produce any
     * abbreviation variants.
     * 
     * TODO: allow caller to get all entries, including abbreviations.
     *
     * @param index
     *            solr instance to query
     * @return country data hash
     * @throws SolrServerException
     *             the solr server exception
     * @throws IOException
     *             on err, if country metadata file is not found in classpath
     */
public static Map<String, Country> loadCountries(SolrServer index) throws SolrServerException, IOException {
    GeonamesUtility geodataUtil = new GeonamesUtility();
    Map<String, Country> countryCodeMap = geodataUtil.getISOCountries();
    Logger log = LoggerFactory.getLogger(SolrGazetteer.class);
    ModifiableSolrParams ctryparams = new ModifiableSolrParams();
    ctryparams.set(CommonParams.FL, "id,name,cc,FIPS_cc,ISO3_cc,adm1,adm2,feat_class,feat_code,geo,name_type");
    /* TODO: Consider different behaviors for PCLI vs. PCL[DFS] */
    ctryparams.set("q", "+feat_class:A +feat_code:(PCLI OR PCLIX OR TERR) +name_type:N");
    /* As of 2015 we have 2300+ name variants for countries and territories */
    ctryparams.set("rows", 5000);
    QueryResponse response = index.query(ctryparams);
    // Process Solr Response
    //
    SolrDocumentList docList = response.getResults();
    for (SolrDocument gazEntry : docList) {
        Country C = createCountry(gazEntry);
        Country existingCountry = countryCodeMap.get(C.getCountryCode());
        if (existingCountry != null) {
            if (existingCountry.ownsTerritory(C.getName())) {
            // do nothing.
            } else if (C.isTerritory) {
                log.debug("{} territory of {}", C, existingCountry);
                existingCountry.addTerritory(C);
            } else {
                log.debug("{} alias of {}", C, existingCountry);
                // all other metadata is same.
                existingCountry.addAlias(C.getName());
            }
            continue;
        }
        log.info("Unknown country in gazetteer, that is not in flat files. C={}", C);
        countryCodeMap.put(C.getCountryCode(), C);
        countryCodeMap.put(C.CC_ISO3, C);
    }
    return countryCodeMap;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) GeonamesUtility(org.opensextant.util.GeonamesUtility) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Country(org.opensextant.data.Country) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Logger(org.slf4j.Logger) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)161 SolrDocument (org.apache.solr.common.SolrDocument)89 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)52 Test (org.junit.Test)52 NamedList (org.apache.solr.common.util.NamedList)36 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)33 SolrQuery (org.apache.solr.client.solrj.SolrQuery)32 ArrayList (java.util.ArrayList)29 SolrParams (org.apache.solr.common.params.SolrParams)28 SolrServerException (org.apache.solr.client.solrj.SolrServerException)20 SolrInputDocument (org.apache.solr.common.SolrInputDocument)18 IOException (java.io.IOException)15 Map (java.util.Map)14 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)13 HashMap (java.util.HashMap)12 List (java.util.List)10 Date (java.util.Date)8 LinkedHashMap (java.util.LinkedHashMap)8 SolrClient (org.apache.solr.client.solrj.SolrClient)8