Search in sources :

Example 1 with FieldInfo

use of org.apache.solr.client.solrj.response.LukeResponse.FieldInfo in project teiid by teiid.

the class TestSolrMetadataProcessor method buildField.

private FieldInfo buildField(String name, String type, EnumSet<FieldFlag> flags) {
    FieldInfo info = Mockito.mock(FieldInfo.class);
    Mockito.stub(info.getName()).toReturn(name);
    Mockito.stub(info.getType()).toReturn(type);
    Mockito.stub(info.getFlags()).toReturn(flags);
    return info;
}
Also used : FieldInfo(org.apache.solr.client.solrj.response.LukeResponse.FieldInfo)

Example 2 with FieldInfo

use of org.apache.solr.client.solrj.response.LukeResponse.FieldInfo in project ddf by codice.

the class SchemaFieldResolver method getSchemaField.

public SchemaField getSchemaField(String propertyName, boolean isSearchedAsExactValue) {
    SchemaField schemaField = null;
    LukeRequest luke = new LukeRequest();
    LukeResponse rsp;
    try {
        rsp = luke.process(solr.getClient());
        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()) {
                // 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 | SolrException | IOException e) {
        LOGGER.info("Exception while processing LukeRequest", e);
    }
    LOGGER.debug("Did not find SchemaField for property {}", propertyName);
    return schemaField;
}
Also used : LukeRequest(org.apache.solr.client.solrj.request.LukeRequest) LukeResponse(org.apache.solr.client.solrj.response.LukeResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) FieldInfo(org.apache.solr.client.solrj.response.LukeResponse.FieldInfo) SolrException(org.apache.solr.common.SolrException)

Example 3 with FieldInfo

use of org.apache.solr.client.solrj.response.LukeResponse.FieldInfo in project teiid by teiid.

the class SolrMetadataProcessor method getConnectorMetadata.

public void getConnectorMetadata(SolrConnection conn, MetadataFactory metadataFactory) throws TranslatorException {
    int count = 0;
    LukeRequest request = new LukeRequest();
    request.setShowSchema(true);
    LukeResponse response = conn.metadata(request);
    Map<String, FieldInfo> fields = response.getFieldInfo();
    Table table = metadataFactory.addTable(conn.getCoreName());
    table.setSupportsUpdate(true);
    for (String name : fields.keySet()) {
        FieldInfo field = fields.get(name);
        EnumSet<FieldFlag> flags = field.getFlags();
        if ((!name.startsWith("_") && !name.endsWith("_")) || name.startsWith("*") || name.endsWith("*")) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            if (flags.contains(FieldFlag.INDEXED) && flags.contains(FieldFlag.STORED)) {
                Column column = null;
                // array type
                if (flags.contains(FieldFlag.MULTI_VALUED)) {
                    // $NON-NLS-1$
                    column = metadataFactory.addColumn(field.getName(), resolveType(field.getType()) + "[]", table);
                } else {
                    column = metadataFactory.addColumn(field.getName(), resolveType(field.getType()), table);
                }
                column.setUpdatable(true);
                column.setSearchType(SearchType.Searchable);
                // create primary key; and unique keys
                if (field.getDistinct() > 0 || field.getName().equals("id")) {
                    // $NON-NLS-1$
                    if (table.getPrimaryKey() == null) {
                        // $NON-NLS-1$
                        metadataFactory.addPrimaryKey("PK0", Arrays.asList(field.getName()), table);
                    } else {
                        // $NON-NLS-1$
                        metadataFactory.addIndex("UI" + count, true, Arrays.asList(field.getName()), table);
                        count++;
                    }
                }
            }
        }
    }
}
Also used : LukeRequest(org.apache.solr.client.solrj.request.LukeRequest) LukeResponse(org.apache.solr.client.solrj.response.LukeResponse) Table(org.teiid.metadata.Table) FieldFlag(org.apache.solr.common.luke.FieldFlag) Column(org.teiid.metadata.Column) FieldInfo(org.apache.solr.client.solrj.response.LukeResponse.FieldInfo)

Example 4 with FieldInfo

use of org.apache.solr.client.solrj.response.LukeResponse.FieldInfo in project teiid by teiid.

the class TestSolrMetadataProcessor method testMetadata.

@Test
public void testMetadata() throws TranslatorException {
    SolrMetadataProcessor mp = new SolrMetadataProcessor();
    MetadataFactory mf = new MetadataFactory("vdb", 1, "solr", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    SolrConnection conn = Mockito.mock(SolrConnection.class);
    Mockito.stub(conn.getCoreName()).toReturn("SomeTable");
    LinkedHashMap<String, FieldInfo> fields = new LinkedHashMap<String, LukeResponse.FieldInfo>();
    fields.put("col1", buildField("col1", "string", EnumSet.of(FieldFlag.STORED, FieldFlag.INDEXED)));
    fields.put("col2", buildField("col2", "int", EnumSet.of(FieldFlag.STORED, FieldFlag.INDEXED)));
    fields.put("col3", buildField("col3", "int", EnumSet.of(FieldFlag.STORED, FieldFlag.INDEXED, FieldFlag.MULTI_VALUED)));
    fields.put("id", buildField("id", "long", EnumSet.of(FieldFlag.STORED, FieldFlag.INDEXED)));
    LukeResponse response = Mockito.mock(LukeResponse.class);
    ;
    Mockito.stub(response.getFieldInfo()).toReturn(fields);
    Mockito.stub(conn.metadata(Mockito.any(LukeRequest.class))).toReturn(response);
    mp.process(mf, conn);
    String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    String expected = "CREATE FOREIGN TABLE SomeTable (\n" + "	col1 string OPTIONS (SEARCHABLE 'Searchable'),\n" + "	col2 integer OPTIONS (SEARCHABLE 'Searchable'),\n" + "	col3 integer[] OPTIONS (SEARCHABLE 'Searchable'),\n" + "	id long OPTIONS (SEARCHABLE 'Searchable'),\n" + "	CONSTRAINT PK0 PRIMARY KEY(id)\n" + ") OPTIONS (UPDATABLE TRUE);";
    assertEquals(expected, metadataDDL);
}
Also used : LukeResponse(org.apache.solr.client.solrj.response.LukeResponse) LukeRequest(org.apache.solr.client.solrj.request.LukeRequest) MetadataFactory(org.teiid.metadata.MetadataFactory) Properties(java.util.Properties) FieldInfo(org.apache.solr.client.solrj.response.LukeResponse.FieldInfo) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

FieldInfo (org.apache.solr.client.solrj.response.LukeResponse.FieldInfo)4 LukeRequest (org.apache.solr.client.solrj.request.LukeRequest)3 LukeResponse (org.apache.solr.client.solrj.response.LukeResponse)3 IOException (java.io.IOException)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 SolrException (org.apache.solr.common.SolrException)1 FieldFlag (org.apache.solr.common.luke.FieldFlag)1 Test (org.junit.Test)1 Column (org.teiid.metadata.Column)1 MetadataFactory (org.teiid.metadata.MetadataFactory)1 Table (org.teiid.metadata.Table)1