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(String q) 

Source Link

Document

Create a new SolrQuery

Usage

From source file:ddf.catalog.source.solr.SolrCatalogProvider.java

License:Open Source License

@Override
public Set<ContentType> getContentTypes() {

    Set<ContentType> finalSet = new HashSet<>();

    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeFormat.STRING, true);
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION, AttributeFormat.STRING,
            true);/*  ww w. j ava 2s . c o m*/

    /*
     * If we didn't find the field, it most likely means it does not exist. If it does not
     * exist, then we can safely say that no content types are in this catalog provider
     */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }

    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);

    try {
        QueryResponse solrResponse = server.query(query, METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {

            // if no content types have an associated version, the list of pivot fields will be
            // empty.
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");

                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // Only one facet field was added. That facet field may contain multiple
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentTypeImpl contentType = new ContentTypeImpl(currContentType.getName(), null);

                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {

                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName:{}", contentTypeName);

                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}",
                                contentTypeName);
                        ContentTypeImpl contentType = new ContentTypeImpl(contentTypeName, null);

                        finalSet.add(contentType);

                    } else {
                        for (PivotField innerPf : pf.getPivot()) {

                            LOGGER.debug("contentTypeVersion:{}. For contentTypeName: {}", innerPf.getValue(),
                                    contentTypeName);

                            ContentTypeImpl contentType = new ContentTypeImpl(contentTypeName,
                                    innerPf.getValue().toString());

                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }

    } catch (SolrServerException e) {
        LOGGER.info("SOLR server exception getting content types", e);
    }

    return finalSet;
}

From source file:ddf.catalog.source.solr.SolrCatalogProvider.java

License:Open Source License

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    if (updateRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }// w  w w. j  a va 2 s .c  om

    // for the modified date, possibly will be replaced by a plugin?
    Date now = new Date();

    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();

    // the list of updates, both new and old metacards
    ArrayList<Update> updateList = new ArrayList<>();

    String attributeName = updateRequest.getAttributeName();

    // need an attribute name in order to do query
    if (attributeName == null) {
        throw new IngestException(
                "Attribute name cannot be null. " + "Please provide the name of the attribute.");
    }

    List<String> identifiers = new ArrayList<>();

    // if we have nothing to update, send the empty list
    if (updates == null || updates.size() == 0) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<Update>());
    }

    /* 1. QUERY */

    // Loop to get all identifiers
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        identifiers.add(updateEntry.getKey().toString());
    }

    /* 1a. Create the old Metacard Query */
    String attributeQuery = getQuery(attributeName, identifiers);

    SolrQuery query = new SolrQuery(attributeQuery);

    QueryResponse idResults = null;

    /* 1b. Execute Query */
    try {
        idResults = server.query(query, METHOD.POST);
    } catch (SolrServerException e) {
        LOGGER.warn("SOLR server exception during query", e);
    }

    // CHECK if we got any results back
    if (idResults != null && idResults.getResults() != null && idResults.getResults().size() != 0) {

        LOGGER.info("Found {} current metacard(s).", idResults.getResults().size());

        // CHECK updates size assertion
        if (idResults.getResults().size() > updates.size()) {
            throw new IngestException(
                    "Found more metacards than updated metacards provided. Please ensure your attribute values match unique records.");
        }

    } else {
        LOGGER.info("No results found for given attribute values.");

        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<Update>());

    }

    /*
     * According to HashMap javadoc, if initialCapacity > (max entries / load factor), then no
     * rehashing will occur. We purposely calculate the correct capacity for no rehashing.
     */

    /*
     * A map is used to store the metacards so that the order of metacards returned will not
     * matter. If we use a List and the metacards are out of order, we might not match the new
     * metacards properly with the old metacards.
     */
    int initialHashMapCapacity = (int) (idResults.getResults().size() / HASHMAP_DEFAULT_LOAD_FACTOR) + 1;

    // map of old metacards to be populated
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>(initialHashMapCapacity);

    /* 1c. Populate list of old metacards */
    for (SolrDocument doc : idResults.getResults()) {
        Metacard old;
        try {
            old = client.createMetacard(doc);
        } catch (MetacardCreationException e) {
            throw new IngestException("Could not create metacard(s).");
        }

        if (!idToMetacardMap.containsKey(old.getAttribute(attributeName).getValue())) {
            idToMetacardMap.put(old.getAttribute(attributeName).getValue(), old);
        } else {
            throw new IngestException("The attribute value given [" + old.getAttribute(attributeName).getValue()
                    + "] matched multiple records. Attribute values must at most match only one unique Metacard.");
        }
    }

    /* 2. Update the cards */
    List<Metacard> newMetacards = new ArrayList<>();
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        String localKey = updateEntry.getKey().toString();

        /* 2a. Prepare new Metacard */
        MetacardImpl newMetacard = new MetacardImpl(updateEntry.getValue());
        // Find the exact oldMetacard that corresponds with this newMetacard
        Metacard oldMetacard = idToMetacardMap.get(localKey);

        // We need to skip because of partial updates such as one entry
        // matched but another did not
        if (oldMetacard != null) {
            prepareForUpdate(now, oldMetacard.getId(), newMetacard, oldMetacard);

            newMetacard.setSourceId(getId());

            newMetacards.add(newMetacard);
            updateList.add(new UpdateImpl(newMetacard, oldMetacard));
        }

    }

    try {
        client.add(newMetacards, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        throw new IngestException("Server could not ingest metacard(s).");
    }

    return new UpdateResponseImpl(updateRequest, null, updateList);
}

From source file:ddf.catalog.source.solr.SolrCatalogProvider.java

License:Open Source License

@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws IngestException {
    if (deleteRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }//from  w  w w.j av a 2 s .  com

    List<Metacard> deletedMetacards = new ArrayList<>();

    String attributeName = deleteRequest.getAttributeName();
    if (StringUtils.isBlank(attributeName)) {
        throw new IngestException("Attribute name cannot be empty. Please provide the name of the attribute.");
    }

    @SuppressWarnings("unchecked")
    List<? extends Serializable> identifiers = deleteRequest.getAttributeValues();

    if (identifiers == null || identifiers.size() == 0) {
        return new DeleteResponseImpl(deleteRequest, null, deletedMetacards);
    }

    /* 1. Query first for the records */
    String fieldName = attributeName + SchemaFields.TEXT_SUFFIX;
    SolrQuery query = new SolrQuery(client.getIdentifierQuery(fieldName, identifiers));
    query.setRows(identifiers.size());

    QueryResponse solrResponse;
    try {
        solrResponse = server.query(query, METHOD.POST);
    } catch (SolrServerException e) {
        LOGGER.info("SOLR server exception deleting request message", e);
        throw new IngestException(COULD_NOT_COMPLETE_DELETE_REQUEST_MESSAGE);
    }

    SolrDocumentList docs = solrResponse.getResults();

    for (SolrDocument doc : docs) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("SOLR DOC: {}", doc.getFieldValue(Metacard.ID + SchemaFields.TEXT_SUFFIX));
        }

        try {
            deletedMetacards.add(client.createMetacard(doc));
        } catch (MetacardCreationException e) {
            LOGGER.info("Metacard creation exception creating metacards during delete", e);
            throw new IngestException("Could not create metacard(s).");
        }

    }

    /* 2. Delete */
    try {
        // the assumption is if something was deleted, it should be gone
        // right away, such as expired data, etc.
        // so we force the commit
        client.deleteByIds(fieldName, identifiers, true);
    } catch (SolrServerException | IOException e) {
        throw new IngestException(COULD_NOT_COMPLETE_DELETE_REQUEST_MESSAGE);
    }

    return new DeleteResponseImpl(deleteRequest, null, deletedMetacards);
}

From source file:ddf.catalog.source.solr.SolrFilterDelegate.java

License:Open Source License

@Override
public SolrQuery propertyIsLike(String propertyName, String pattern, boolean isCaseSensitive) {
    verifyInputData(propertyName, pattern);

    String mappedPropertyName = getMappedPropertyName(propertyName, AttributeFormat.STRING, false);
    if (isCaseSensitive) {
        mappedPropertyName = resolver.getCaseSensitiveField(mappedPropertyName);
    }/* ww  w  . j av a  2  s .  c  o  m*/
    String searchPhrase = escapeSpecialCharacters(pattern);
    if (!searchPhrase.contains(SOLR_WILDCARD_CHAR) && !searchPhrase.contains(SOLR_SINGLE_WILDCARD_CHAR)) {
        // Not an exact phrase
        searchPhrase = QUOTE + searchPhrase + QUOTE;

        return new SolrQuery(mappedPropertyName + ":" + searchPhrase);
    } else {
        if (Metacard.ANY_TEXT.equals(propertyName)) {
            if (isCaseSensitive) {
                return new SolrQuery(WHITESPACE_TOKENIZED_METADATA_FIELD + SchemaFields.HAS_CASE + ":("
                        + searchPhrase + ")");
            } else {
                return new SolrQuery(WHITESPACE_TOKENIZED_METADATA_FIELD + ":(" + searchPhrase + ")");
            }
        } else {
            return new SolrQuery(mappedPropertyName + ":(" + searchPhrase + ")");
        }
    }

}

From source file:ddf.catalog.source.solr.SolrFilterDelegate.java

License:Open Source License

private SolrQuery getSolrQueryWithSort(String givenSpatialString) {

    if (sortBy != null && sortBy.getPropertyName() != null
            && Result.DISTANCE.equals(sortBy.getPropertyName().getPropertyName())) {

        String spatialQueryWithDistance = SCORE_DISTANCE + givenSpatialString;

        SolrQuery solrQuery = new SolrQuery(spatialQueryWithDistance);

        solrQuery.setFields("*", "score");

        ORDER sortOrder = ORDER.asc;/*from w  ww  . j  av a  2  s .c om*/

        if (SortOrder.DESCENDING.equals(sortBy.getSortOrder())) {
            sortOrder = ORDER.desc;
        }

        solrQuery.addSort("score", sortOrder);

        return new SolrQuery(spatialQueryWithDistance);

    } else {
        return new SolrQuery(givenSpatialString);
    }
}

From source file:ddf.catalog.source.solr.SolrMetacardClientImpl.java

License:Open Source License

@Override
public Set<ContentType> getContentTypes() {
    Set<ContentType> finalSet = new HashSet<>();

    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeType.AttributeFormat.STRING,
            true);/*from   www  . ja v  a2s .c om*/
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION,
            AttributeType.AttributeFormat.STRING, true);

    /*
     * If we didn't find the field, it most likely means it does not exist. If it does not
     * exist, then we can safely say that no content types are in this catalog provider
     */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }

    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);

    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Map.Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {

            // if no content types have an associated version, the list of pivot fields will be
            // empty.
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");

                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // Only one facet field was added. That facet field may contain multiple
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentType contentType = new ContentTypeImpl(currContentType.getName(), null);

                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {

                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName: {}", contentTypeName);

                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}",
                                contentTypeName);
                        ContentType contentType = new ContentTypeImpl(contentTypeName, null);

                        finalSet.add(contentType);

                    } else {
                        for (PivotField innerPf : pf.getPivot()) {

                            LOGGER.debug("contentTypeVersion: {}. For contentTypeName: {}", innerPf.getValue(),
                                    contentTypeName);

                            ContentType contentType = new ContentTypeImpl(contentTypeName,
                                    innerPf.getValue().toString());

                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }

    } catch (SolrServerException | IOException e) {
        LOGGER.info("Solr exception getting content types", e);
    }

    return finalSet;
}

From source file:de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java

License:Apache License

@Override
public SolrDocumentList getHierarchyNodes(String ingestId, String providerId) {
    StringBuilder querySb = new StringBuilder(INGEST_ID + ":\"" + ingestId + "\" AND " + PROVIDER_ID + ":");
    if ("*".equals(providerId)) {
        querySb.append(providerId);//from www .j  a v a2 s.  c  o m
    } else {
        querySb.append("\"" + providerId + "\"");
    }
    SolrQuery query = new SolrQuery(querySb.toString());
    query.setSort(SortClause.create(POSITION, SolrQuery.ORDER.asc));
    query.setStart(0);
    query.setRows(Integer.MAX_VALUE);

    try {
        QueryResponse response = this.solrServer.query(query);
        return response.getResults();
    } catch (SolrServerException e) {
        throw new NodeStoreException(
                "Could not retrieve nodes for ingest " + ingestId + " and provider " + providerId, e);
    }
}

From source file:de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java

License:Apache License

@Override
public SolrDocument getHierarchyNode(String id) {
    SolrQuery parentQuery = new SolrQuery(ID + ":\"" + id + "\"");
    parentQuery.setRows(1);/*from ww w  .java 2 s.com*/
    try {
        QueryResponse response = this.solrServer.query(parentQuery);
        SolrDocumentList results = response.getResults();
        if (results.size() > 1) {
            LOG.error("Found more than 1 parent for the node with id {}", id);
            throw new NodeStoreException("Found more than 1 parent for the node with id " + id);
        } else if (results.isEmpty()) {
            return null;
        } else {
            SolrDocument document = results.get(0);
            return document;
        }
    } catch (SolrServerException e) {
        throw new NodeStoreException("Could not retrieve the node with id " + id, e);
    }
}

From source file:de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java

License:Apache License

@Override
public SolrDocumentList getHierarchyChildren(String id, Query filterQuery) {
    SolrQuery childQuery = new SolrQuery(PARENT + ":\"" + id + "\"");
    if (!Strings.isNullOrEmpty(filterQuery.getQuery()) && !filterQuery.getQuery().equals(Query.ALL_QUERY)) {
        childQuery.addFilterQuery(filterQuery.getQuery());
    }/*w ww .  ja  v a  2 s  .co  m*/
    childQuery.setStart(filterQuery.getOffset());
    childQuery.setRows(filterQuery.getRows());
    childQuery.setSort(SortClause.create(POSITION, SolrQuery.ORDER.asc));

    try {
        QueryResponse response = this.solrServer.query(childQuery);
        return response.getResults();
    } catch (SolrServerException e) {
        throw new NodeStoreException("Could not retrieve the node with id " + id, e);
    }
}

From source file:de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java

License:Apache License

@Override
public long hasChildren(String id) {
    SolrQuery childQuery = new SolrQuery(PARENT + ":\"" + id + "\"");
    childQuery.setStart(0);//from  w w  w  .j a  va  2 s . c  om
    childQuery.setRows(1);

    try {
        QueryResponse response = this.solrServer.query(childQuery);
        return response.getResults().getNumFound();
    } catch (SolrServerException e) {
        throw new NodeStoreException("Could not determine the number of children for the node with id " + id,
                e);
    }
}