public SolrInputDocument toSolrInputDocument(SolrDocument d) { final SolrInputDocument doc = new SolrInputDocument(); for (String name : d.getFieldNames()) { doc.addField(name, d.getFieldValue(name)); } return doc; } }
private static void addFieldToSolrDocument(SolrInputDocument inputDocument,String fieldName,Object fieldValue,List<String> fieldsToIndex){ if ((!fieldsToIndex.isEmpty() && fieldsToIndex.contains(fieldName)) || fieldsToIndex.isEmpty()){ inputDocument.addField(fieldName, fieldValue); } }
private void commitDocumentChanges(String collectionName, Collection<SolrInputDocument> documents) throws SolrServerException, IOException { if (documents.size() == 0) return; try { solrClient.request(newUpdateRequest().add(documents), collectionName); } catch (HttpSolrClient.RemoteSolrException rse) { logger.error("Unable to save documents to Solr as one of the shape objects stored were not compatible with Solr.", rse); logger.error("Details in failed document batch: "); for (SolrInputDocument d : documents) { Collection<String> fieldNames = d.getFieldNames(); for (String name : fieldNames) { logger.error(name + ":" + d.getFieldValue(name).toString()); } } throw rse; } }
private void deleteIndividualFieldsFromIndex(String collectionName, String keyIdField, String docId, HashSet<IndexEntry> fieldDeletions) throws SolrServerException, IOException { if (fieldDeletions.isEmpty()) return; Map<String, String> fieldDeletes = new HashMap<String, String>(1) {{ put("set", null); }}; SolrInputDocument doc = new SolrInputDocument(); doc.addField(keyIdField, docId); StringBuilder sb = new StringBuilder(); for (IndexEntry fieldToDelete : fieldDeletions) { doc.addField(fieldToDelete.field, fieldDeletes); sb.append(fieldToDelete).append(","); } if (logger.isTraceEnabled()) logger.trace("Deleting individual fields [{}] for document {}", sb.toString(), docId); UpdateRequest singleDocument = newUpdateRequest(); singleDocument.add(doc); solrClient.request(singleDocument, collectionName); }
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))));
entry = new HashMap<String, ByteIterator>((int) results.getNumFound()); for (String field : hit.getFieldNames()) { entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
logger.debug("Executed query [{}] in {} ms", query.getQuery(), response.getElapsedTime()); int totalHits = response.getResults().size(); if (!query.hasLimit() && totalHits >= maxResults) { logger.warn("Query result set truncated to first [{}] elements for query: {}", maxResults, query); double score = Double.parseDouble(hit.getFieldValue("score").toString()); result.add(new RawQuery.Result<String>(hit.getFieldValue(keyIdField).toString(), score));
@Override public SolrInputDocument buildDocument(final Indexable indexable, List<IndexField> fields, List<Locale> locales) { final SolrInputDocument document = new SolrInputDocument(); attachBasicDocumentFields(indexable, document); attachIndexableDocumentFields(document, indexable, fields, locales); attachAdditionalDocumentFields(indexable, document); extensionManager.getProxy().attachChildDocuments(indexable, document, fields, locales); return document; }
/** * Walk the category hierarchy upwards, adding a field for each level to the solr document * * @param document the solr document for the product * @param cache the catalog structure cache * @param categoryId the current category id */ protected void buildFullCategoryHierarchy(SolrInputDocument document, CatalogStructure cache, Long categoryId, Set<Long> indexedParents) { Long catIdToAdd = shs.getCategoryId(categoryId); Collection<Object> existingValues = document.getFieldValues(shs.getCategoryFieldName()); if (existingValues == null || !existingValues.contains(catIdToAdd)) { document.addField(shs.getCategoryFieldName(), catIdToAdd); } Set<Long> parents = cache.getParentCategoriesByCategory().get(categoryId); for (Long parent : parents) { if (!indexedParents.contains(parent)) { indexedParents.add(parent); buildFullCategoryHierarchy(document, cache, parent, indexedParents); } } }
/** * Given a list of Sku IDs from solr, this method will look up the IDs via the skuDao and build out * actual Sku instances. It will return a Sku list that is sorted by the order of the IDs in the passed * in list. * * @param response * @return the actual Sku instances as a result of the search */ protected List<Sku> getSkus(List<SolrDocument> responseDocuments) { final List<Long> skuIds = new ArrayList<>(); for (SolrDocument doc : responseDocuments) { skuIds.add((Long) doc.getFieldValue(shs.getIndexableIdFieldName())); } List<Sku> skus = skuDao.readSkusByIds(skuIds); extensionManager.getProxy().batchFetchCatalogDataForSkus(skus); // We have to sort the skus list by the order of the skuIds list to maintain sortability in the UI if (skus != null) { Collections.sort(skus, new Comparator<Sku>() { @Override public int compare(Sku o1, Sku o2) { return new Integer(skuIds.indexOf(o1.getId())).compareTo(skuIds.indexOf(o2.getId())); } }); } return skus; }
public Status insert(String table, String key, Map<String, ByteIterator> values) { try { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", key); for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.addField(entry.getKey(), entry.getValue());
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))));
entry = new HashMap<>((int) results.getNumFound()); for (String field : hit.getFieldNames()) { entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
private void addMultivalueFieldToDoc(SolrInputDocument doc, String tupleField, ITuple tuple) { String[] values = getValues(tupleField, tuple); for (String value : values) { log.debug("Adding {} to multivalue field document {}", value, tupleField); doc.addField(tupleField, value); } }
logger.debug("Executed query [{}] in {} ms", query.getCondition(), response.getElapsedTime()); int totalHits = response.getResults().size(); result.add(hit.getFieldValue(keyIdField).toString());
private SolrInputDocument buildDocument(ITuple tuple) { SolrInputDocument doc = new SolrInputDocument(); for (String tupleField : tuple.getFields()) { FieldTypeWrapper fieldTypeWrapper = typeFinder.getFieldTypeWrapper(tupleField); if (fieldTypeWrapper != null) { FieldType fieldType = fieldTypeWrapper.getType(); if (fieldType.isMultiValued()) { addMultivalueFieldToDoc(doc, tupleField, tuple); } else { addFieldToDoc(doc, tupleField, tuple); } } else { log.debug("Field [{}] does NOT match static or dynamic field declared in schema. Not added to document", tupleField); } } return doc; }
/** * Given a list of product IDs from solr, this method will look up the IDs via the productDao and build out * actual Product instances. It will return a Products that is sorted by the order of the IDs in the passed * in list. * * @param response * @return the actual Product instances as a result of the search */ protected List<Product> getProducts(List<SolrDocument> responseDocuments) { final List<Long> productIds = new ArrayList<>(); for (SolrDocument doc : responseDocuments) { productIds.add((Long) doc.getFieldValue(shs.getIndexableIdFieldName())); } List<Product> products = productDao.readProductsByIds(productIds); extensionManager.getProxy().batchFetchCatalogData(products); // We have to sort the products list by the order of the productIds list to maintain sortability in the UI if (products != null) { Collections.sort(products, new Comparator<Product>() { @Override public int compare(Product o1, Product o2) { Long o1id = shs.getIndexableId(o1); Long o2id = shs.getIndexableId(o2); return new Integer(productIds.indexOf(o1id)).compareTo(productIds.indexOf(o2id)); } }); } extensionManager.getProxy().modifySearchResults(responseDocuments, products); return products; }
public Status insert(String table, String key, Map<String, ByteIterator> values) { try { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", key); for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.addField(entry.getKey(), entry.getValue());
private void addFieldToDoc(SolrInputDocument doc, String tupleField, ITuple tuple) { Object val = getValue(tupleField, tuple); log.debug("Adding to document (field, val) = ({}, {})", tupleField, val); doc.addField(tupleField, val); }
public Status update(String table, String key, Map<String, ByteIterator> values) { try { SolrInputDocument updatedDoc = new SolrInputDocument(); updatedDoc.addField("id", key); updatedDoc.addField(entry.getKey(), Collections.singletonMap("set", entry.getValue()));