Search in sources :

Example 1 with MapField

use of org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField in project lucene-solr by apache.

the class UIMAToSolrMapper method map.

/**
   * map features of a certain UIMA type to corresponding Solr fields based on the mapping
   *
   * @param typeName             name of UIMA type to map
   */
void map(String typeName, Map<String, MapField> featureFieldsmapping) throws FieldMappingException {
    try {
        Type type = cas.getTypeSystem().getType(typeName);
        for (FSIterator<FeatureStructure> iterator = cas.getFSIndexRepository().getAllIndexedFS(type); iterator.hasNext(); ) {
            FeatureStructure fs = iterator.next();
            for (String featureName : featureFieldsmapping.keySet()) {
                MapField mapField = featureFieldsmapping.get(featureName);
                String fieldNameFeature = mapField.getFieldNameFeature();
                String fieldNameFeatureValue = fieldNameFeature == null ? null : fs.getFeatureValueAsString(type.getFeatureByBaseName(fieldNameFeature));
                String fieldName = mapField.getFieldName(fieldNameFeatureValue);
                if (log.isInfoEnabled()) {
                    log.info("mapping {}@{} to {}", new Object[] { typeName, featureName, fieldName });
                }
                String featureValue;
                if (fs instanceof Annotation && "coveredText".equals(featureName)) {
                    featureValue = ((Annotation) fs).getCoveredText();
                } else {
                    featureValue = fs.getFeatureValueAsString(type.getFeatureByBaseName(featureName));
                }
                if (log.isDebugEnabled()) {
                    log.debug("writing {} in {}", new Object[] { featureValue, fieldName });
                }
                document.addField(fieldName, featureValue);
            }
        }
    } catch (Exception e) {
        throw new FieldMappingException(e);
    }
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) Annotation(org.apache.uima.jcas.tcas.Annotation) MapField(org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField)

Example 2 with MapField

use of org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField in project lucene-solr by apache.

the class UIMAUpdateRequestProcessorTest method testMultiMap.

@Test
public void testMultiMap() {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(UIMA_MULTI_MAP_CHAIN);
    assertNotNull(chained);
    UIMAUpdateRequestProcessorFactory factory = (UIMAUpdateRequestProcessorFactory) chained.getProcessors().get(0);
    assertNotNull(factory);
    UpdateRequestProcessor processor = factory.getInstance(req(), null, null);
    assertTrue(processor instanceof UIMAUpdateRequestProcessor);
    SolrUIMAConfiguration conf = ((UIMAUpdateRequestProcessor) processor).getConfiguration();
    Map<String, Map<String, MapField>> map = conf.getTypesFeaturesFieldsMapping();
    Map<String, MapField> subMap = map.get("a-type-which-can-have-multiple-features");
    assertEquals(2, subMap.size());
    assertEquals("1", subMap.get("A").getFieldName(null));
    assertEquals("2", subMap.get("B").getFieldName(null));
}
Also used : SolrCore(org.apache.solr.core.SolrCore) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) Map(java.util.Map) MapField(org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField) Test(org.junit.Test)

Example 3 with MapField

use of org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField in project lucene-solr by apache.

the class SolrUIMAConfigurationReader method readTypesFeaturesFieldsMapping.

@SuppressWarnings("rawtypes")
private Map<String, Map<String, MapField>> readTypesFeaturesFieldsMapping() {
    Map<String, Map<String, MapField>> map = new HashMap<>();
    NamedList fieldMappings = (NamedList) args.get("fieldMappings");
    /* iterate over UIMA types */
    for (int i = 0; i < fieldMappings.size(); i++) {
        NamedList type = (NamedList) fieldMappings.get("type", i);
        String typeName = (String) type.get("name");
        Map<String, MapField> subMap = new HashMap<>();
        /* iterate over mapping definitions */
        for (int j = 0; j < type.size() - 1; j++) {
            NamedList mapping = (NamedList) type.get("mapping", j + 1);
            String featureName = (String) mapping.get("feature");
            String fieldNameFeature = null;
            String mappedFieldName = (String) mapping.get("field");
            if (mappedFieldName == null) {
                fieldNameFeature = (String) mapping.get("fieldNameFeature");
                mappedFieldName = (String) mapping.get("dynamicField");
            }
            if (mappedFieldName == null)
                throw new RuntimeException("either of field or dynamicField should be defined for feature " + featureName);
            MapField mapField = new MapField(mappedFieldName, fieldNameFeature);
            subMap.put(featureName, mapField);
        }
        map.put(typeName, subMap);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) Map(java.util.Map) HashMap(java.util.HashMap) MapField(org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField)

Aggregations

MapField (org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField)3 Map (java.util.Map)2 HashMap (java.util.HashMap)1 NamedList (org.apache.solr.common.util.NamedList)1 SolrCore (org.apache.solr.core.SolrCore)1 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)1 UpdateRequestProcessorChain (org.apache.solr.update.processor.UpdateRequestProcessorChain)1 FeatureStructure (org.apache.uima.cas.FeatureStructure)1 Type (org.apache.uima.cas.Type)1 Annotation (org.apache.uima.jcas.tcas.Annotation)1 Test (org.junit.Test)1