Example usage for org.apache.solr.client.solrj.request LukeRequest LukeRequest

List of usage examples for org.apache.solr.client.solrj.request LukeRequest LukeRequest

Introduction

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

Prototype

public LukeRequest() 

Source Link

Usage

From source file:com.github.fengtan.sophie.beans.SolrUtils.java

License:Open Source License

/**
 * Get list of instantiated fields from the remote Solr server.
 * // ww w  . j  a  va2 s. co m
 * Dynamic fields are named according to how they are instantiated (e.g.
 * dynamic_foobar).
 * 
 * @return List of fields defined on the remote Solr server.
 * @throws SophieException
 *             If the remote fields cannot be fetched.
 */
public static List<FieldInfo> getRemoteFields() throws SophieException {
    LukeRequest request = new LukeRequest();
    try {
        LukeResponse response = request.process(Sophie.client);
        return new ArrayList<FieldInfo>(response.getFieldInfo().values());
    } catch (SolrServerException | IOException | SolrException e) {
        throw new SophieException("Unable to fetch list of Solr fields", e);
    }
}

From source file:com.github.fengtan.sophie.beans.SolrUtils.java

License:Open Source License

/**
 * Get a list of declared fields from the remote Solr server.
 * //from   w ww  .  jav  a2s. c  o  m
 * Dynamic fields are named according to how they are declared so may be
 * globs (e.g. dynamic_*).
 * 
 * @return Map of fields keyed by field name.
 * @throws SophieException
 *             If the remote fields cannot be fetched.
 */
@SuppressWarnings("unchecked")
public static Map<String, FieldInfo> getRemoteSchemaFields() throws SophieException {
    // Send request.
    LukeRequest request = new LukeRequest();
    request.setShowSchema(true);
    LukeResponse response;
    try {
        response = request.process(Sophie.client);
    } catch (SolrServerException | IOException | SolrException e) {
        throw new SophieException("Unable to fetch list of Solr fields", e);
    }
    // Extract regular fields.
    Map<String, FieldInfo> fields = new HashMap<String, FieldInfo>();
    fields.putAll(response.getFieldInfo());
    // Extract dynamic fields.
    NamedList<Object> schema = (NamedList<Object>) response.getResponse().get("schema");
    if (schema != null) {
        NamedList<Object> dynamicFields = (NamedList<Object>) schema.get("dynamicFields");
        if (dynamicFields != null) {
            for (Map.Entry<String, Object> dynamicField : dynamicFields) {
                FieldInfo fieldInfo = new FieldInfo(dynamicField.getKey());
                fieldInfo.read((NamedList<Object>) dynamicField.getValue());
                fields.put(dynamicField.getKey(), fieldInfo);
            }
        }
    }
    return fields;
}

From source file:com.sindicetech.siren.solr.schema.TestExtendedJsonField.java

License:Open Source License

@Test
public void testLukeRequest() throws IOException, SolrServerException {
    LukeRequest req = new LukeRequest();
    req.setShowSchema(true);//from  w w w  . j ava 2 s .  c  o  m
    final LukeResponse response = req.process(this.getWrapper().getServer());
    assertTrue(!response.getFieldInfo().isEmpty());
}

From source file:fr.cnes.sitools.metacatalogue.resources.opensearch.OpensearchDescribeResource.java

License:Open Source License

/**
 * Create a description for the metacatalogue containing all the request parameters, their types and enumeration
 * /*from  w  w  w  .j a  v a 2 s.co  m*/
 * @return a description for the metacatalogue
 */
private Describe createDescribe() {
    Describe describe = new Describe();
    SolrServer server = SolRUtils.getSolRServer(solrCoreUrl);
    if (server == null) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
                "Solr core : " + solrCoreUrl + " not reachable");
    }

    LukeRequest request = new LukeRequest();
    // request.setNumTerms(maxTopTerms);

    try {
        List<Filter> filters = new ArrayList<Filter>();
        LukeResponse response = request.process(server);
        int numDocs = response.getNumDocs();

        Map<String, LukeResponse.FieldInfo> fields = response.getFieldInfo();

        for (Entry<String, LukeResponse.FieldInfo> field : fields.entrySet()) {
            LukeResponse.FieldInfo fieldInfo = field.getValue();
            String fieldName = fieldInfo.getName();

            boolean indexed = false;

            EnumSet<FieldFlag> flags = FieldInfo.parseFlags(fieldInfo.getSchema());
            indexed = (flags != null && flags.contains(FieldFlag.INDEXED));

            if (indexed && addToDescription(fieldName)) {

                // make a terms query to get the top terms
                SolrQuery query = new SolrQuery();
                query.setRequestHandler("/terms");
                query.setTerms(true);
                query.addTermsField(fieldName);
                query.setTermsLimit(maxTopTerms);
                query.setTermsMinCount(1);

                QueryRequest termsRequest = new QueryRequest(query);
                TermsResponse termsResponse = termsRequest.process(server).getTermsResponse();

                List<Term> terms = termsResponse.getTerms(fieldName);

                Filter filter = new Filter();
                filter.setId(fieldName);
                filter.setTitle(fieldName);
                if (canBeCategorised(terms)) {
                    filter.setType(FilterType.enumeration);
                    filter.setPopulation(numDocs);
                    filter.setSon(createSons(terms));
                } else {
                    filter.setType(getFilterType(fieldInfo.getType()));
                }
                filters.add(filter);
            }
        }
        describe.setFilters(filters);
        return describe;
    } catch (SolrServerException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage(), e);
    } catch (IOException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage(), e);
    }
}

From source file:fr.cnes.sitools.metacatalogue.resources.opensearch.OpensearchDescriptionServiceResource.java

License:Open Source License

private String buildTemplateURL() {

    SolrServer server = SolRUtils.getSolRServer(solrCoreUrl);
    if (server == null) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
                "Solr core : " + solrCoreUrl + " not reachable");
    }/*www  .j  a va 2  s.  co  m*/

    LukeRequest request = new LukeRequest();
    request.setNumTerms(maxTopTerms);

    try {
        String description = "/search?q={searchTerms}&amp;format=json";

        description += addQueryParameter(OpenSearchQuery.START_PAGE, false);
        description += addQueryParameter(OpenSearchQuery.START_INDEX, false);
        description += addQueryParameter(OpenSearchQuery.COUNT, false);
        description += addQueryParameter(OpenSearchQuery.GEO_BOX, false);

        description += addQueryParameter(OpenSearchQuery.LANGUAGE, false);
        description += addQueryParameter(OpenSearchQuery.TIME_START, false);
        description += addQueryParameter(OpenSearchQuery.TIME_END, false);
        description += addQueryParameter(OpenSearchQuery.MODIFIED, false);

        LukeResponse response = request.process(server);
        Map<String, LukeResponse.FieldInfo> fields = response.getFieldInfo();
        for (Entry<String, LukeResponse.FieldInfo> field : fields.entrySet()) {
            LukeResponse.FieldInfo fieldInfo = field.getValue();
            String fieldName = fieldInfo.getName();

            boolean indexed = false;
            EnumSet<FieldFlag> flags = FieldInfo.parseFlags(fieldInfo.getSchema());
            if (flags != null && flags.contains(FieldFlag.INDEXED)) {
                indexed = true;
            }
            if (indexed && addToDescription(fieldName)) {
                description += "&amp;" + fieldName + "={ptsc:" + fieldName + "?}";
            }
        }

        return description;
    } catch (SolrServerException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage(), e);
    } catch (IOException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage(), e);
    }

}

From source file:net.hydromatic.optiq.impl.solr.SolrTable.java

License:Apache License

/** Deduces the names and types of a table's columns by reading the first line
 * of a CSV file. *///  ww  w  .  j ava2  s . c  om
static RelDataType deduceRowType(JavaTypeFactory typeFactory, SolrServer srv, List<SolrFieldType> fieldTypes) {
    final List<RelDataType> types = new ArrayList<RelDataType>();
    names = new ArrayList<String>();

    LukeRequest lukeRequest = new LukeRequest();
    lukeRequest.setNumTerms(1);
    LukeResponse lukeResponse = null;
    try {
        lukeResponse = lukeRequest.process(srv);
    } catch (SolrServerException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    List<LukeResponse.FieldInfo> sorted = new ArrayList<LukeResponse.FieldInfo>(
            lukeResponse.getFieldInfo().values());

    for (LukeResponse.FieldInfo infoEntry : sorted) {

        SolrFieldType fieldType = SolrFieldType.of(infoEntry.getType().toUpperCase());
        String name = infoEntry.getName();

        final RelDataType type;
        if (fieldType == null) {
            type = typeFactory.createJavaType(String.class);
        } else {
            type = fieldType.toType(typeFactory);
        }
        names.add(name);
        types.add(type);
        fieldTypes.add(fieldType);
    }

    if (names.isEmpty()) {
        names.add("line");
        types.add(typeFactory.createJavaType(String.class));
    }
    return typeFactory.createStructType(Pair.zip(names, types));
}

From source file:net.yacy.cora.federate.solr.connector.SolrServerConnector.java

License:Open Source License

private LukeResponse getIndexBrowser(final boolean showSchema) throws SolrServerException {
    // get all fields contained in index
    final LukeRequest lukeRequest = new LukeRequest();
    lukeRequest.setResponseParser(new XMLResponseParser());
    lukeRequest.setNumTerms(0);//from w  w  w .j a  v  a2  s  .co  m
    lukeRequest.setShowSchema(showSchema);
    LukeResponse lukeResponse = null;
    try {
        lukeResponse = lukeRequest.process(this.server);
    } catch (IOException e) {
        throw new SolrServerException(e.getMessage());
    }
    return lukeResponse;
}

From source file:org.apache.metron.solr.dao.SolrColumnMetadataDao.java

License:Apache License

protected List<Map<String, Object>> getIndexFields(String index) throws IOException, SolrServerException {
    List<Map<String, Object>> indexFields = new ArrayList<>();

    // Get all the fields in use, including dynamic fields
    LukeRequest lukeRequest = new LukeRequest();
    LukeResponse lukeResponse = lukeRequest.process(client, index);
    for (Entry<String, LukeResponse.FieldInfo> field : lukeResponse.getFieldInfo().entrySet()) {
        Map<String, Object> fieldData = new HashMap<>();
        fieldData.put("name", field.getValue().getName());
        fieldData.put("type", field.getValue().getType());
        indexFields.add(fieldData);/* ww  w.  j av a 2 s .com*/

    }

    // Get all the schema fields
    SchemaRepresentation schemaRepresentation = new SchemaRequest().process(client, index)
            .getSchemaRepresentation();
    indexFields.addAll(schemaRepresentation.getFields());

    return indexFields;
}

From source file:org.codice.solr.query.SchemaFieldResolver.java

License:Open Source License

public SchemaField getSchemaField(String propertyName, boolean isSearchedAsExactValue) {
    SchemaField schemaField = null;//from   ww w .j a  v a2 s. c o m
    LukeRequest luke = new LukeRequest();
    LukeResponse rsp;
    try {
        rsp = luke.process(solr);
        Map<String, FieldInfo> fieldsInfo = rsp.getFieldInfo();
        if (fieldsInfo != null && !fieldsInfo.isEmpty()) {
            LOGGER.debug("got fieldsInfo for {} fields", fieldsInfo.size());

            for (Map.Entry<String, FieldInfo> entry : fieldsInfo.entrySet()) {

                // See if any fieldName startsWith(propertyName)
                // if it does, then see if remainder of fieldName matches any expected suffix
                // if suffix matches, then get type of field and cache it
                if (entry.getKey().startsWith(propertyName)
                        && StringUtils.endsWithAny(entry.getKey(), FORMAT_SUFFIXES)) {
                    String fieldType = entry.getValue().getType();
                    int index = StringUtils.lastIndexOfAny(entry.getKey(), FORMAT_SUFFIXES);
                    String suffix = entry.getKey().substring(index);
                    if (!isSearchedAsExactValue) {
                        suffix = getSpecialIndexSuffix(suffix);
                        fieldType += suffix;
                    }
                    LOGGER.debug("field {} has type {}", entry.getKey(), fieldType);
                    schemaField = new SchemaField(entry.getKey(), fieldType);
                    schemaField.setSuffix(suffix);
                    return schemaField;
                }
            }
        } else {
            LOGGER.debug("fieldsInfo from LukeRequest are either null or empty");
        }

    } catch (SolrServerException e) {
        LOGGER.info("SolrServerException while processing LukeRequest", e);
    } catch (IOException e) {
        LOGGER.info("IOException while processing LukeRequest", e);
    }

    LOGGER.debug("Did not find SchemaField for property {}", propertyName);

    return schemaField;
}

From source file:org.craftercms.commerce.server.solr.SolrIndex.java

License:Open Source License

/**
 * @return A set of all the field names found in the Solr index
 *///from  ww  w  . j  a v  a 2 s  . c om
public Set<String> getFields() {
    Set<String> fields = new HashSet<String>(0);
    try {
        LukeRequest lukeRequest = new LukeRequest();
        LukeResponse lukeResponse = lukeRequest.process(solrServer);
        Map<String, FieldInfo> map = lukeResponse.getFieldInfo();
        fields = map.keySet();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Error retrieving schema info from Solr", e);
    }
    return fields;
}