Example usage for org.apache.solr.client.solrj SolrQuery addField

List of usage examples for org.apache.solr.client.solrj SolrQuery addField

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery addField.

Prototype

public SolrQuery addField(String field) 

Source Link

Usage

From source file:org.ambraproject.service.article.BrowseServiceImpl.java

License:Apache License

/**
 * Returns list of articles in a given date range, from newest to oldest
 * @param params the collection class of parameters.
 * @return the articles/*from  ww  w. j  a  va 2 s.  c o  m*/
 */
private BrowseResult getArticlesByDateViaSolr(BrowseParameters params) {
    BrowseResult result = new BrowseResult();
    ArrayList<SearchHit> articles = new ArrayList<SearchHit>();
    long totalSize = 0;

    SolrQuery query = createCommonQuery(params.getJournalKey());

    query.addField("title_display");
    query.addField("author_display");
    query.addField("article_type");
    query.addField("publication_date");
    query.addField("id");
    query.addField("abstract_primary_display");
    query.addField("eissn");
    query.addField("striking_image");

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String sDate = sdf.format(params.getStartDate().getTime());
    String eDate = sdf.format(params.getEndDate().getTime());

    sDate = sDate + "T00:00:00Z";
    eDate = eDate + "T00:00:00Z";

    query.addFilterQuery("publication_date:[" + sDate + " TO " + eDate + "]");

    StringBuffer sb = new StringBuffer();
    if (params.getArticleTypes() != null && params.getArticleTypes().size() > 0) {
        for (URI uri : params.getArticleTypes()) {
            String path = uri.getPath();
            int index = path.lastIndexOf("/");
            if (index != -1) {
                String articleType = path.substring(index + 1);
                sb.append("\"").append(articleType).append("\"").append(" OR ");
            }
        }
        String articleTypesQuery = sb.substring(0, sb.length() - 4);

        if (articleTypesQuery.length() > 0) {
            query.addFilterQuery("article_type_facet:" + articleTypesQuery);
        }
    }

    setSort(query, params);

    query.setStart(params.getPageNum() * params.getPageSize());
    query.setRows(params.getPageSize());

    log.info("getArticlesByDate Solr Query:" + query.toString());

    try {
        QueryResponse response = this.serverFactory.getServer().query(query);
        SolrDocumentList documentList = response.getResults();
        totalSize = documentList.getNumFound();

        for (SolrDocument document : documentList) {
            SearchHit sh = createArticleBrowseDisplay(document, query.toString());
            articles.add(sh);
        }

    } catch (SolrServerException e) {
        log.error("Unable to execute a query on the Solr Server.", e);
    }

    result.setArticles(articles);
    result.setTotal(totalSize);

    return result;
}

From source file:org.apache.nifi.processors.solr.GetSolr.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

    final ComponentLog logger = getLogger();
    final AtomicBoolean continuePaging = new AtomicBoolean(true);
    final SolrQuery solrQuery = new SolrQuery();

    try {/*w w w . j a  va 2 s  . co m*/
        if (id_field == null) {
            id_field = getFieldNameOfUniqueKey();
        }

        final String dateField = context.getProperty(DATE_FIELD).getValue();

        final Map<String, String> stateMap = new HashMap<String, String>();
        stateMap.putAll(context.getStateManager().getState(Scope.CLUSTER).toMap());

        solrQuery.setQuery("*:*");
        final String query = context.getProperty(SOLR_QUERY).getValue();
        if (!StringUtils.isBlank(query) && !query.equals("*:*")) {
            solrQuery.addFilterQuery(query);
        }
        final StringBuilder automatedFilterQuery = (new StringBuilder()).append(dateField).append(":[")
                .append(stateMap.get(STATE_MANAGER_FILTER)).append(" TO *]");
        solrQuery.addFilterQuery(automatedFilterQuery.toString());

        final List<String> fieldList = new ArrayList<String>();
        final String returnFields = context.getProperty(RETURN_FIELDS).getValue();
        if (!StringUtils.isBlank(returnFields)) {
            fieldList.addAll(Arrays.asList(returnFields.trim().split("[,]")));
            if (!fieldList.contains(dateField)) {
                fieldList.add(dateField);
                dateFieldNotInSpecifiedFieldsList.set(true);
            }
            for (String returnField : fieldList) {
                solrQuery.addField(returnField.trim());
            }
        }

        solrQuery.setParam(CursorMarkParams.CURSOR_MARK_PARAM, stateMap.get(STATE_MANAGER_CURSOR_MARK));
        solrQuery.setRows(context.getProperty(BATCH_SIZE).asInteger());

        final StringBuilder sortClause = (new StringBuilder()).append(dateField).append(" asc,")
                .append(id_field).append(" asc");
        solrQuery.setParam("sort", sortClause.toString());

        while (continuePaging.get()) {
            final QueryRequest req = new QueryRequest(solrQuery);
            if (isBasicAuthEnabled()) {
                req.setBasicAuthCredentials(getUsername(), getPassword());
            }

            logger.debug(solrQuery.toQueryString());
            final QueryResponse response = req.process(getSolrClient());
            final SolrDocumentList documentList = response.getResults();

            if (response.getResults().size() > 0) {
                final SolrDocument lastSolrDocument = documentList.get(response.getResults().size() - 1);
                final String latestDateValue = df.format(lastSolrDocument.get(dateField));
                final String newCursorMark = response.getNextCursorMark();

                solrQuery.setParam(CursorMarkParams.CURSOR_MARK_PARAM, newCursorMark);
                stateMap.put(STATE_MANAGER_CURSOR_MARK, newCursorMark);
                stateMap.put(STATE_MANAGER_FILTER, latestDateValue);

                FlowFile flowFile = session.create();
                flowFile = session.putAttribute(flowFile, "solrQuery", solrQuery.toString());

                if (context.getProperty(RETURN_TYPE).getValue().equals(MODE_XML.getValue())) {
                    if (dateFieldNotInSpecifiedFieldsList.get()) {
                        for (SolrDocument doc : response.getResults()) {
                            doc.removeFields(dateField);
                        }
                    }
                    flowFile = session.write(flowFile,
                            SolrUtils.getOutputStreamCallbackToTransformSolrResponseToXml(response));
                    flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(),
                            "application/xml");

                } else {
                    final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER)
                            .asControllerService(RecordSetWriterFactory.class);
                    final RecordSchema schema = writerFactory.getSchema(null, null);
                    final RecordSet recordSet = SolrUtils.solrDocumentsToRecordSet(response.getResults(),
                            schema);
                    final StringBuffer mimeType = new StringBuffer();
                    flowFile = session.write(flowFile, new OutputStreamCallback() {
                        @Override
                        public void process(final OutputStream out) throws IOException {
                            try {
                                final RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema,
                                        out);
                                writer.write(recordSet);
                                writer.flush();
                                mimeType.append(writer.getMimeType());
                            } catch (SchemaNotFoundException e) {
                                throw new ProcessException("Could not parse Solr response", e);
                            }
                        }
                    });
                    flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(),
                            mimeType.toString());
                }
                session.transfer(flowFile, REL_SUCCESS);
            }
            continuePaging.set(response.getResults().size() == Integer
                    .parseInt(context.getProperty(BATCH_SIZE).getValue()));
        }
        context.getStateManager().setState(stateMap, Scope.CLUSTER);
    } catch (SolrServerException | SchemaNotFoundException | IOException e) {
        context.yield();
        session.rollback();
        logger.error("Failed to execute query {} due to {}", new Object[] { solrQuery.toString(), e }, e);
        throw new ProcessException(e);
    } catch (final Throwable t) {
        context.yield();
        session.rollback();
        logger.error("Failed to execute query {} due to {}", new Object[] { solrQuery.toString(), t }, t);
        throw t;
    }
}

From source file:org.apache.nifi.processors.solr.QuerySolr.java

License:Apache License

@Override
public void doOnTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();

    FlowFile flowFileOriginal = session.get();
    FlowFile flowFileResponse;//  w w  w .  jav  a 2 s  .  c  o  m

    if (flowFileOriginal == null) {
        if (context.hasNonLoopConnection()) {
            return;
        }
        flowFileResponse = session.create();
    } else {
        flowFileResponse = session.create(flowFileOriginal);
    }

    final SolrQuery solrQuery = new SolrQuery();
    final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue());
    final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFileResponse)
            .getValue();

    final StringBuilder transitUri = new StringBuilder("solr://");
    transitUri.append(getSolrLocation());
    if (isSolrCloud) {
        transitUri.append(":").append(collection);
    }
    final StopWatch timer = new StopWatch(false);

    try {
        solrQuery.setQuery(context.getProperty(SOLR_PARAM_QUERY).evaluateAttributeExpressions(flowFileResponse)
                .getValue());
        solrQuery.setRequestHandler(context.getProperty(SOLR_PARAM_REQUEST_HANDLER)
                .evaluateAttributeExpressions(flowFileResponse).getValue());

        if (context.getProperty(SOLR_PARAM_FIELD_LIST).isSet()) {
            for (final String field : context.getProperty(SOLR_PARAM_FIELD_LIST)
                    .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) {
                solrQuery.addField(field.trim());
            }
        }

        // Avoid ArrayIndexOutOfBoundsException due to incorrectly configured sorting
        try {
            if (context.getProperty(SOLR_PARAM_SORT).isSet()) {
                final List<SolrQuery.SortClause> sortings = new ArrayList<>();
                for (final String sorting : context.getProperty(SOLR_PARAM_SORT)
                        .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) {
                    final String[] sortEntry = sorting.trim().split(" ");
                    sortings.add(new SolrQuery.SortClause(sortEntry[0], sortEntry[1]));
                }
                solrQuery.setSorts(sortings);
            }
        } catch (Exception e) {
            throw new ProcessException("Error while parsing the sort clauses for the Solr query");
        }

        final Integer startParam = context.getProperty(SOLR_PARAM_START).isSet()
                ? Integer.parseInt(context.getProperty(SOLR_PARAM_START)
                        .evaluateAttributeExpressions(flowFileResponse).getValue())
                : CommonParams.START_DEFAULT;

        solrQuery.setStart(startParam);

        final Integer rowParam = context.getProperty(SOLR_PARAM_ROWS).isSet()
                ? Integer.parseInt(context.getProperty(SOLR_PARAM_ROWS)
                        .evaluateAttributeExpressions(flowFileResponse).getValue())
                : CommonParams.ROWS_DEFAULT;

        solrQuery.setRows(rowParam);

        final Map<String, String[]> additionalSolrParams = SolrUtils.getRequestParams(context,
                flowFileResponse);

        final Set<String> searchComponents = extractSearchComponents(additionalSolrParams);
        solrQuery.add(new MultiMapSolrParams(additionalSolrParams));

        final Map<String, String> attributes = new HashMap<>();
        attributes.put(ATTRIBUTE_SOLR_CONNECT, getSolrLocation());
        if (isSolrCloud) {
            attributes.put(ATTRIBUTE_SOLR_COLLECTION, collection);
        }
        attributes.put(ATTRIBUTE_SOLR_QUERY, solrQuery.toString());
        if (flowFileOriginal != null) {
            flowFileOriginal = session.putAllAttributes(flowFileOriginal, attributes);
        }

        flowFileResponse = session.putAllAttributes(flowFileResponse, attributes);

        final boolean getEntireResults = RETURN_ALL_RESULTS
                .equals(context.getProperty(AMOUNT_DOCUMENTS_TO_RETURN).getValue());
        boolean processFacetsAndStats = true;
        boolean continuePaging = true;

        while (continuePaging) {

            timer.start();

            Map<String, String> responseAttributes = new HashMap<>();
            responseAttributes.put(ATTRIBUTE_SOLR_START, solrQuery.getStart().toString());
            responseAttributes.put(ATTRIBUTE_SOLR_ROWS, solrQuery.getRows().toString());

            if (solrQuery.getStart() > UPPER_LIMIT_START_PARAM) {
                logger.warn(
                        "The start parameter of Solr query {} exceeded the upper limit of {}. The query will not be processed "
                                + "to avoid performance or memory issues on the part of Solr.",
                        new Object[] { solrQuery.toString(), UPPER_LIMIT_START_PARAM });
                flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes);
                timer.stop();
                break;
            }

            final QueryRequest req = new QueryRequest(solrQuery);
            if (isBasicAuthEnabled()) {
                req.setBasicAuthCredentials(getUsername(), getPassword());
            }

            final QueryResponse response = req.process(getSolrClient());
            timer.stop();

            final Long totalNumberOfResults = response.getResults().getNumFound();

            responseAttributes.put(ATTRIBUTE_SOLR_NUMBER_RESULTS, totalNumberOfResults.toString());
            responseAttributes.put(ATTRIBUTE_CURSOR_MARK, response.getNextCursorMark());
            responseAttributes.put(ATTRIBUTE_SOLR_STATUS, String.valueOf(response.getStatus()));
            responseAttributes.put(ATTRIBUTE_QUERY_TIME, String.valueOf(response.getQTime()));
            flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes);

            if (response.getResults().size() > 0) {

                if (context.getProperty(RETURN_TYPE).getValue().equals(MODE_XML.getValue())) {
                    flowFileResponse = session.write(flowFileResponse,
                            SolrUtils.getOutputStreamCallbackToTransformSolrResponseToXml(response));
                    flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(),
                            MIME_TYPE_XML);
                } else {
                    final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER)
                            .evaluateAttributeExpressions(flowFileResponse)
                            .asControllerService(RecordSetWriterFactory.class);
                    final RecordSchema schema = writerFactory.getSchema(flowFileResponse.getAttributes(), null);
                    final RecordSet recordSet = SolrUtils.solrDocumentsToRecordSet(response.getResults(),
                            schema);
                    final StringBuffer mimeType = new StringBuffer();
                    final FlowFile flowFileResponseRef = flowFileResponse;
                    flowFileResponse = session.write(flowFileResponse, out -> {
                        try (final RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out,
                                flowFileResponseRef)) {
                            writer.write(recordSet);
                            writer.flush();
                            mimeType.append(writer.getMimeType());
                        } catch (SchemaNotFoundException e) {
                            throw new ProcessException("Could not parse Solr response", e);
                        }
                    });
                    flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(),
                            mimeType.toString());
                }

                if (processFacetsAndStats) {
                    if (searchComponents.contains(FacetParams.FACET)) {
                        FlowFile flowFileFacets = session.create(flowFileResponse);
                        flowFileFacets = session.write(flowFileFacets, out -> {
                            try (final OutputStreamWriter osw = new OutputStreamWriter(out);
                                    final JsonWriter writer = new JsonWriter(osw)) {
                                addFacetsFromSolrResponseToJsonWriter(response, writer);
                            }
                        });
                        flowFileFacets = session.putAttribute(flowFileFacets, CoreAttributes.MIME_TYPE.key(),
                                MIME_TYPE_JSON);
                        session.getProvenanceReporter().receive(flowFileFacets, transitUri.toString(),
                                timer.getDuration(TimeUnit.MILLISECONDS));
                        session.transfer(flowFileFacets, FACETS);
                    }

                    if (searchComponents.contains(StatsParams.STATS)) {
                        FlowFile flowFileStats = session.create(flowFileResponse);
                        flowFileStats = session.write(flowFileStats, out -> {
                            try (final OutputStreamWriter osw = new OutputStreamWriter(out);
                                    final JsonWriter writer = new JsonWriter(osw)) {
                                addStatsFromSolrResponseToJsonWriter(response, writer);
                            }
                        });
                        flowFileStats = session.putAttribute(flowFileStats, CoreAttributes.MIME_TYPE.key(),
                                MIME_TYPE_JSON);
                        session.getProvenanceReporter().receive(flowFileStats, transitUri.toString(),
                                timer.getDuration(TimeUnit.MILLISECONDS));
                        session.transfer(flowFileStats, STATS);
                    }
                    processFacetsAndStats = false;
                }
            }

            if (getEntireResults) {
                final Integer totalDocumentsReturned = solrQuery.getStart() + solrQuery.getRows();
                if (totalDocumentsReturned < totalNumberOfResults) {
                    solrQuery.setStart(totalDocumentsReturned);
                    session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(),
                            timer.getDuration(TimeUnit.MILLISECONDS));
                    session.transfer(flowFileResponse, RESULTS);
                    flowFileResponse = session.create(flowFileResponse);
                } else {
                    continuePaging = false;
                }
            } else {
                continuePaging = false;
            }
        }

    } catch (Exception e) {
        flowFileResponse = session.penalize(flowFileResponse);
        flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION, e.getClass().getName());
        flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION_MESSAGE, e.getMessage());
        session.transfer(flowFileResponse, FAILURE);
        logger.error("Failed to execute query {} due to {}. FlowFile will be routed to relationship failure",
                new Object[] { solrQuery.toString(), e }, e);
        if (flowFileOriginal != null) {
            flowFileOriginal = session.penalize(flowFileOriginal);
        }
    }

    if (!flowFileResponse.isPenalized()) {
        session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(),
                timer.getDuration(TimeUnit.MILLISECONDS));
        session.transfer(flowFileResponse, RESULTS);
    }

    if (flowFileOriginal != null) {
        if (!flowFileOriginal.isPenalized()) {
            session.transfer(flowFileOriginal, ORIGINAL);
        } else {
            session.remove(flowFileOriginal);
        }
    }
}

From source file:org.dspace.app.cris.ws.WSServicesCRIS.java

public void internalBuildFieldList(SolrQuery solrQuery, String... projection) {
    solrQuery.setFields(projection);/*from   w  ww  .  jav a2  s  .c om*/
    solrQuery.addField("search.resourceid");
    solrQuery.addField("search.resourcetype");
}

From source file:org.dspace.app.cris.ws.WSServicesPublications.java

public void internalBuildFieldList(SolrQuery solrQuery, String... projection) {

    solrQuery.addField("handle");
    solrQuery.addField("search.resourceid");

    for (String otherToIndex : CrisWebservicesExtraIndexPlugin.OTHERS_TO_INDEX) {
        solrQuery.addField(otherToIndex);
    }/* w ww . ja  v  a2  s  .c  om*/

    for (int j = 0; j < projection.length; j++) {
        solrQuery.addField(projection[j]);
    }

}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

/**
 * Parse the passed query.//from w  w  w. j av a 2  s  .  c  o  m
 * 
 * @param query
 *        string
 * @return the parsed query
 * @throws ParseException
 *         when the parsing brakes
 */
@Override
protected Iterable<? extends DocumentScore> query(Resource subject, String query, URI propertyURI,
        boolean highlight) throws MalformedQueryException, IOException {
    SolrQuery q = prepareQuery(propertyURI, new SolrQuery(query));
    if (highlight) {
        q.setHighlight(true);
        String field = (propertyURI != null) ? SearchFields.getPropertyField(propertyURI) : "*";
        q.addHighlightField(field);
        q.setHighlightSimplePre(SearchFields.HIGHLIGHTER_PRE_TAG);
        q.setHighlightSimplePost(SearchFields.HIGHLIGHTER_POST_TAG);
        q.setHighlightSnippets(2);
    }

    QueryResponse response;
    if (q.getHighlight()) {
        q.addField("*");
    } else {
        q.addField(SearchFields.URI_FIELD_NAME);
    }
    q.addField("score");
    try {
        if (subject != null) {
            response = search(subject, q);
        } else {
            response = search(q);
        }
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
    SolrDocumentList results = response.getResults();
    final Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
    return Iterables.transform(results, new Function<SolrDocument, DocumentScore>() {

        @Override
        public DocumentScore apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            Map<String, List<String>> docHighlighting = (highlighting != null) ? highlighting.get(doc.getId())
                    : null;
            return new SolrDocumentScore(doc, docHighlighting);
        }
    });
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

@Override
protected Iterable<? extends DocumentDistance> geoQuery(URI geoProperty, Point p, final URI units,
        double distance, String distanceVar, Var contextVar) throws MalformedQueryException, IOException {
    double kms = GeoUnits.toKilometres(distance, units);

    String qstr = "{!geofilt score=recipDistance}";
    if (contextVar != null) {
        Resource ctx = (Resource) contextVar.getValue();
        String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
        if (ctx != null) {
            qstr = tq + " AND " + qstr;
        } else {/*from   ww  w .j  a va2 s  .c  o m*/
            qstr = "-" + tq + " AND " + qstr;
        }
    }
    SolrQuery q = new SolrQuery(qstr);
    q.set(SpatialParams.FIELD, SearchFields.getPropertyField(geoProperty));
    q.set(SpatialParams.POINT, p.getY() + "," + p.getX());
    q.set(SpatialParams.DISTANCE, Double.toString(kms));
    q.addField(SearchFields.URI_FIELD_NAME);
    // ':' is part of the fl parameter syntax so we can't use the full
    // property field name
    // instead we use wildcard + local part of the property URI
    q.addField("*" + geoProperty.getLocalName());
    // always include the distance - needed for sanity checking
    q.addField(DISTANCE_FIELD + ":geodist()");
    boolean requireContext = (contextVar != null && !contextVar.hasValue());
    if (requireContext) {
        q.addField(SearchFields.CONTEXT_FIELD_NAME);
    }

    QueryResponse response;
    try {
        response = search(q);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }

    SolrDocumentList results = response.getResults();
    return Iterables.transform(results, new Function<SolrDocument, DocumentDistance>() {

        @Override
        public DocumentDistance apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            return new SolrDocumentDistance(doc, units);
        }
    });
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

@Override
protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, URI geoProperty, Shape shape,
        Var contextVar) throws MalformedQueryException, IOException {
    String spatialOp = toSpatialOp(relation);
    if (spatialOp == null) {
        return null;
    }//from ww  w .  j  a  va2s  .  c om
    String wkt = toWkt(shape);
    String qstr = "\"" + spatialOp + "(" + wkt + ")\"";
    if (contextVar != null) {
        Resource ctx = (Resource) contextVar.getValue();
        String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
        if (ctx != null) {
            qstr = tq + " AND " + qstr;
        } else {
            qstr = "-" + tq + " AND " + qstr;
        }
    }
    SolrQuery q = new SolrQuery(qstr);
    q.set(CommonParams.DF, SearchFields.getPropertyField(geoProperty));
    q.addField(SearchFields.URI_FIELD_NAME);
    // ':' is part of the fl parameter syntax so we can't use the full
    // property field name
    // instead we use wildcard + local part of the property URI
    q.addField("*" + geoProperty.getLocalName());
    boolean requireContext = (contextVar != null && !contextVar.hasValue());
    if (requireContext) {
        q.addField(SearchFields.CONTEXT_FIELD_NAME);
    }

    QueryResponse response;
    try {
        response = search(q);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }

    SolrDocumentList results = response.getResults();
    return Iterables.transform(results, new Function<SolrDocument, DocumentResult>() {

        @Override
        public DocumentResult apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            return new SolrDocumentResult(doc);
        }
    });
}

From source file:org.geotools.data.solr.SolrDataStore.java

License:Open Source License

/**
 * Builds the SolrJ query with support of subset of fields, limit/offset, sorting, OGC filter
 * encoding and viewParams <br>/*from   www.j a v  a2s .  c  o m*/
 * The SOLR query always need the order by PK field to enable pagination and efficient data
 * retrieving <br>
 * Currently only additional "q" and "fq" SOLR parameters can be passed using vireParams, this
 * conditions are added in AND with others
 * 
 * @param featureType the feature type to query
 * @param q the OGC query to translate in SOLR request
 * 
 * @see {@link Hints#VIRTUAL_TABLE_PARAMETERS}
 * 
 */
protected SolrQuery select(SimpleFeatureType featureType, Query q) {
    SolrQuery query = new SolrQuery();
    query.setParam("omitHeader", true);
    try {
        // Column names
        if (q.getPropertyNames() != null) {
            for (String prop : q.getPropertyNames()) {
                query.addField(prop);
            }
        }
        query.setQuery("*:*");

        // Encode limit/offset, if necessary
        if (q.getStartIndex() != null && q.getStartIndex() >= 0) {
            query.setStart(q.getStartIndex());
        }
        if (q.getMaxFeatures() > 0) {
            query.setRows(q.getMaxFeatures());
        }

        // Sort
        ORDER naturalSortOrder = ORDER.asc;
        if (q.getSortBy() != null) {
            for (SortBy sort : q.getSortBy()) {
                if (sort.getPropertyName() != null) {
                    query.addSort(sort.getPropertyName().getPropertyName(),
                            sort.getSortOrder().equals(SortOrder.ASCENDING) ? ORDER.asc : ORDER.desc);
                } else {
                    naturalSortOrder = sort.getSortOrder().equals(SortOrder.ASCENDING) ? ORDER.asc : ORDER.desc;
                }
            }
        }

        // Always add natural sort by PK to support pagination
        query.addSort(getPrimaryKey(featureType.getTypeName()).getName(), naturalSortOrder);

        // Encode OGC filer
        FilterToSolr f2s = initializeFilterToSolr(featureType);
        String fq = this.field + ":" + featureType.getTypeName();
        Filter simplified = SimplifyingFilterVisitor.simplify(q.getFilter());
        String ffq = f2s.encodeToString(simplified);
        if (ffq != null && !ffq.isEmpty()) {
            fq = fq + " AND " + ffq;
        }
        query.setFilterQueries(fq);

        // Add viewpPrams
        addViewparams(q, query);

    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    }
    return query;
}

From source file:org.geotools.data.solr.SolrFeatureReader.java

License:Open Source License

/**
 * Creates the feature reader for SOLR store <br>
 * The feature reader use SOLR CURSOR to paginate request, so multiple SOLR query will be
 * executed/*from   www. ja  v  a  2s.  co  m*/
 * 
 * @param featureType the feature type to query
 * @param solrUrl the URL of SOLR server
 * @param solrQuery the SOLR query to execute
 * @param solrDataStore the SOLR store
 * @throws SolrServerException
 */
public SolrFeatureReader(SimpleFeatureType featureType, HttpSolrServer server, SolrQuery solrQuery,
        SolrDataStore solrDataStore) throws SolrServerException {
    this.featureType = featureType;
    this.solrQuery = solrQuery;
    this.wktReader = new WKTReader();
    this.solrDataStore = solrDataStore;
    this.pkey = solrDataStore.getPrimaryKey(featureType.getTypeName());

    this.builder = new SimpleFeatureBuilder(featureType);

    this.server = server;

    // Add always pk as field if not already present
    if (solrQuery.getFields() != null && !solrQuery.getFields().contains(pkey.getName())) {
        solrQuery.addField(pkey.getName());
    }

    // Can't use pagination with start, then first query for cursor mark of start
    if (solrQuery.getStart() != null && solrQuery.getStart() > 0) {
        cursorMark = getCursorMarkForStart(server, solrQuery);
    } else {
        cursorMark = CursorMarkParams.CURSOR_MARK_START;
    }
    solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
    QueryResponse rsp = server.query(solrQuery);
    if (this.solrDataStore.getLogger().isLoggable(Level.FINE)) {
        this.solrDataStore.getLogger().log(Level.FINE, "SOLR query done: " + solrQuery.toString());
    }
    this.solrDocIterator = rsp.getResults().iterator();
    nextCursorMark = rsp.getNextCursorMark();
    counter = 0;
}