use of org.apache.solr.common.SolrDocumentList in project play-cookbook by spinscale.
the class Query method fetchIds.
public List<String> fetchIds() {
query.setFields("id");
SolrDocumentList results = getResponse();
List<String> ids = new ArrayList(results.size());
for (SolrDocument doc : results) {
String entityId = doc.getFieldValue("id").toString().split(":")[1];
ids.add(entityId);
}
return ids;
}
use of org.apache.solr.common.SolrDocumentList in project ddf by codice.
the class SolrMetacardClientImpl method query.
@Override
public List<Metacard> query(String queryString) throws UnsupportedQueryException {
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
try {
QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
SolrDocumentList docs = solrResponse.getResults();
List<Metacard> results = new ArrayList<>();
for (SolrDocument doc : docs) {
try {
results.add(createMetacard(doc));
} catch (MetacardCreationException e) {
throw new UnsupportedQueryException("Could not create metacard(s).", e);
}
}
return results;
} catch (SolrServerException | IOException e) {
throw new UnsupportedQueryException("Could not complete solr query.", e);
}
}
use of org.apache.solr.common.SolrDocumentList in project ddf by codice.
the class PersistentStoreImpl method get.
@Override
public // Returned Map will have suffixes in the key names - client is responsible for handling them
List<Map<String, Object>> get(String type, String cql) throws PersistenceException {
if (StringUtils.isBlank(type)) {
throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
}
List<Map<String, Object>> results = new ArrayList<>();
// Set Solr Core name to type and create/connect to Solr Core
SolrClient solrClient = getSolrClient(type);
if (solrClient == null) {
throw new PersistenceException("Unable to create Solr client.");
}
SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
try {
SolrQuery solrQuery;
// If not cql specified, then return all items
if (StringUtils.isBlank(cql)) {
solrQuery = new SolrQuery("*:*");
} else {
Filter filter = CQL.toFilter(cql);
solrQuery = (SolrQuery) filter.accept(visitor, null);
}
QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
long numResults = solrResponse.getResults().getNumFound();
LOGGER.debug("numResults = {}", numResults);
SolrDocumentList docs = solrResponse.getResults();
for (SolrDocument doc : docs) {
PersistentItem result = new PersistentItem();
Collection<String> fieldNames = doc.getFieldNames();
for (String name : fieldNames) {
LOGGER.debug("field name = {} has value = {}", name, doc.getFieldValue(name));
if (name.endsWith(PersistentItem.TEXT_SUFFIX) && doc.getFieldValues(name).size() > 1) {
result.addProperty(name, doc.getFieldValues(name).stream().filter(s -> s instanceof String).map(s -> (String) s).collect(Collectors.toSet()));
} else if (name.endsWith(PersistentItem.XML_SUFFIX)) {
result.addXmlProperty(name, (String) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.TEXT_SUFFIX)) {
result.addProperty(name, (String) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.LONG_SUFFIX)) {
result.addProperty(name, (Long) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.INT_SUFFIX)) {
result.addProperty(name, (Integer) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.DATE_SUFFIX)) {
result.addProperty(name, (Date) doc.getFirstValue(name));
} else if (name.endsWith(PersistentItem.BINARY_SUFFIX)) {
result.addProperty(name, (byte[]) doc.getFirstValue(name));
} else {
LOGGER.debug("Not adding field {} because it has invalid suffix", name);
}
}
results.add(result);
}
} catch (CQLException e) {
throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
} catch (SolrServerException | IOException e) {
throw new PersistenceException("SolrServerException while getting Solr data with cql statement " + cql, e);
}
return results;
}
use of org.apache.solr.common.SolrDocumentList in project stanbol by apache.
the class TopicClassificationEngine method suggestTopics.
public List<TopicSuggestion> suggestTopics(String text) throws ClassifierException {
List<TopicSuggestion> suggestedTopics = new ArrayList<TopicSuggestion>(MAX_SUGGESTIONS * 3);
SolrServer solrServer = getActiveSolrServer();
SolrQuery query = new SolrQuery();
query.setRequestHandler("/" + MoreLikeThisParams.MLT);
query.setFilterQueries(entryTypeField + ":" + MODEL_ENTRY);
query.set(MoreLikeThisParams.MATCH_INCLUDE, false);
query.set(MoreLikeThisParams.MIN_DOC_FREQ, 1);
query.set(MoreLikeThisParams.MIN_TERM_FREQ, 1);
query.set(MoreLikeThisParams.MAX_QUERY_TERMS, 30);
query.set(MoreLikeThisParams.MAX_NUM_TOKENS_PARSED, 10000);
// TODO: find a way to parse the interesting terms and report them
// for debugging / explanation in dedicated RDF data structure.
// query.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
query.set(MoreLikeThisParams.SIMILARITY_FIELDS, similarityField);
query.set(CommonParams.STREAM_BODY, text);
// over query the number of suggestions to find a statistical cut based on the curve of the scores of
// the top suggestion
query.setRows(MAX_SUGGESTIONS * 3);
query.setFields(conceptUriField);
query.setIncludeScore(true);
try {
StreamQueryRequest request = new StreamQueryRequest(query);
QueryResponse response = request.process(solrServer);
SolrDocumentList results = response.getResults();
for (SolrDocument result : results.toArray(new SolrDocument[0])) {
String conceptUri = (String) result.getFirstValue(conceptUriField);
if (conceptUri == null) {
throw new ClassifierException(String.format("Solr Core '%s' is missing required field '%s'.", solrCoreId, conceptUriField));
}
Float score = (Float) result.getFirstValue("score");
// fetch metadata
SolrQuery metadataQuery = new SolrQuery("*:*");
// use filter queries to leverage the Solr cache explicitly
metadataQuery.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
metadataQuery.addFilterQuery(conceptUriField + ":" + ClientUtils.escapeQueryChars(conceptUri));
metadataQuery.setFields(conceptUriField, broaderField, primaryTopicUriField);
SolrDocument metadata = solrServer.query(metadataQuery).getResults().get(0);
String primaryTopicUri = (String) metadata.getFirstValue(primaryTopicUriField);
suggestedTopics.add(new TopicSuggestion(conceptUri, primaryTopicUri, metadata.getFieldValues(broaderField), score));
}
} catch (SolrServerException e) {
if ("unknown handler: /mlt".equals(e.getCause().getMessage())) {
String message = String.format("SolrServer with id '%s' for topic engine '%s' lacks" + " configuration for the MoreLikeThisHandler", solrCoreId, engineName);
throw new ClassifierException(message, e);
} else {
throw new ClassifierException(e);
}
}
if (suggestedTopics.size() <= 1) {
// no need to apply the cutting heuristic
return suggestedTopics;
}
// filter out suggestions that are less than some threshold based on the mean of the top scores
float mean = 0.0f;
for (TopicSuggestion suggestion : suggestedTopics) {
mean += suggestion.score / suggestedTopics.size();
}
float threshold = 0.25f * suggestedTopics.get(0).score + 0.75f * mean;
List<TopicSuggestion> filteredSuggestions = new ArrayList<TopicSuggestion>();
for (TopicSuggestion suggestion : suggestedTopics) {
if (filteredSuggestions.size() >= MAX_SUGGESTIONS) {
return filteredSuggestions;
}
if (filteredSuggestions.isEmpty() || suggestion.score > threshold) {
filteredSuggestions.add(suggestion);
} else {
break;
}
}
return filteredSuggestions;
}
use of org.apache.solr.common.SolrDocumentList in project YCSB by brianfrankcooper.
the class SolrClient method scan.
/**
* Perform a range scan for a set of records in the database. Each field/value pair from the
* result will be stored in a HashMap.
*
* @param table
* The name of the table
* @param startkey
* The record key of the first record to read.
* @param recordcount
* The number of records to read
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
* @return Zero on success, a non-zero error code on error. See this class's description for a
* discussion of error codes.
*/
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<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("*:*");
query.setParam("fq", "id:[ " + startkey + " TO * ]");
if (returnFields) {
query.setFields(fieldList);
}
query.setRows(recordcount);
final QueryResponse response = client.query(table, query);
SolrDocumentList results = response.getResults();
HashMap<String, ByteIterator> entry;
for (SolrDocument hit : results) {
entry = new HashMap<String, ByteIterator>((int) results.getNumFound());
for (String field : hit.getFieldNames()) {
entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
}
result.add(entry);
}
return checkStatus(response.getStatus());
} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
return Status.ERROR;
}
Aggregations