Example usage for org.apache.lucene.document Document getField

List of usage examples for org.apache.lucene.document Document getField

Introduction

In this page you can find the example usage for org.apache.lucene.document Document getField.

Prototype

public final IndexableField getField(String name) 

Source Link

Document

Returns a field with the given name if any exist in this document, or null.

Usage

From source file:org.apache.solr.update.DocumentBuilderTest.java

License:Apache License

@Test
public void testWithPolyFieldsAndDocumentBoost() {
    SolrCore core = h.getCore();/*w w  w  . j  a  va  2 s. c  o m*/
    IndexSchema schema = core.getLatestSchema();
    assertFalse(schema.getField("store").omitNorms());
    assertTrue(schema.getField("store_0_coordinate").omitNorms());
    assertTrue(schema.getField("store_1_coordinate").omitNorms());
    assertFalse(schema.getField("amount").omitNorms());
    assertTrue(schema.getField("amount" + FieldType.POLY_FIELD_SEPARATOR + "_currency").omitNorms());
    assertTrue(schema.getField("amount" + FieldType.POLY_FIELD_SEPARATOR + "_amount_raw").omitNorms());

    SolrInputDocument doc = new SolrInputDocument();
    doc.setDocumentBoost(3.0f);
    doc.addField("store", "40.7143,-74.006");
    doc.addField("amount", "10.5");
    Document out = DocumentBuilder.toDocument(doc, schema);
    assertNotNull(out.get("store"));
    assertNotNull(out.getField("store_0_coordinate"));
    //NOTE: As the subtypes have omitNorm=true, they must have boost=1F, otherwise this is going to fail when adding the doc to Lucene.
    assertTrue(1f == out.getField("store_0_coordinate").boost());
    assertTrue(1f == out.getField("store_1_coordinate").boost());
    assertTrue(1f == out.getField("amount" + FieldType.POLY_FIELD_SEPARATOR + "_currency").boost());
    assertTrue(1f == out.getField("amount" + FieldType.POLY_FIELD_SEPARATOR + "_amount_raw").boost());
}

From source file:org.apache.uima.lucas.indexer.DocumentBuilderTest.java

License:Apache License

@Test
public void testCreateDocument() throws CASException, IOException {
    Document document = documentBuilder.createDocument(fields);

    assertEquals(field1, document.getField("field1"));
    assertEquals(field2, document.getField("field2"));
}

From source file:org.ariadne_eu.oai.server.lucene.crosswalk.Lucene2oai_ese.java

License:Creative Commons License

public String createMetadata(Object nativeItem) throws CannotDisseminateFormatException {
    //Cast the nativeItem to your object
    Document doc = (Document) nativeItem;
    List fieldsList = doc.getFields();

    List<org.apache.lucene.document.Field> fieldsNodes = doc.getFields();
    for (org.apache.lucene.document.Field element : fieldsNodes) {
        System.out.println("The fields are:" + element.toString());
    }/*from  w ww  .  j a va2 s  . co  m*/

    Element record = new Element("record", esens);
    record.addNamespaceDeclaration(europeanans);
    record.addNamespaceDeclaration(dcns);

    record.setAttribute("schemaLocation",
            "http://www.europeana.eu/schemas/ese/ http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd", xsi);
    //  record.setAttribute("dc","http://purl.org/dc/elements/1.1/",dcns);

    //dc:identifier
    Element identifier = new Element("identifier", dcns);
    record.addContent(identifier);

    if (doc.getField("lom.general.identifier.entry") != null) {
        String idString = doc.getField("lom.general.identifier.entry").stringValue();
        if (idString != null)
            identifier.setText(idString);
    }

    //dc:title
    Element title = new Element("title", dcns);
    record.addContent(title);

    if (doc.getField("lom.general.title.string") != null) {
        String titleString = doc.getField("lom.general.title.string").stringValue();
        if (titleString != null)
            title.setText(titleString);
    }

    //dc:language

    if (doc.getField("lom.general.language") != null) {

        Element language = new Element("language", dcns);
        record.addContent(language);

        String languageSrt = doc.getField("lom.general.language").stringValue();
        if (languageSrt != null)
            language.setText(languageSrt);
    }

    //dc:description
    Element descr = new Element("description", dcns);
    record.addContent(descr);

    if (doc.getField("lom.general.description.string") != null) {
        String descrString = doc.getField("lom.general.description.string").stringValue();
        if (descrString != null)
            descr.setText(descrString);
    }

    //dc:subject
    Field[] keyNodes = doc.getFields("lom.general.keyword.string");
    for (org.apache.lucene.document.Field element : keyNodes) {
        String theString = element.stringValue();
        if (theString != null && !theString.trim().isEmpty()) {
            Element subject = new Element("subject", dcns);
            record.addContent(subject);
            subject.setText(theString);
        }
    }

    //dc:format
    Element format = new Element("format", dcns);
    record.addContent(format);

    if (doc.getField("lom.technical.format") != null) {
        String formatString = doc.getField("lom.technical.format").stringValue();
        if (formatString != null)
            format.setText(formatString);
    }

    //dcterms:spatial
    if (doc.getFields("lom.general.coverage.string") != null) {
        Field[] spatNodes = doc.getFields("lom.general.coverage.string");
        for (org.apache.lucene.document.Field element : spatNodes) {
            String theString = element.stringValue();
            if (theString != null && !theString.trim().isEmpty()) {
                Element coverage = new Element("spatial", dctermsns);
                record.addContent(coverage);
                coverage.setText(theString);
            }
        }
    }

    //dcterms:temploral
    if (doc.getFields("lom.educational.typicalAgeRange") != null) {
        Field[] tempNodes = doc.getFields("lom.educational.typicalAgeRange");
        for (org.apache.lucene.document.Field element : tempNodes) {
            String theString = element.stringValue();
            if (theString != null && !theString.trim().isEmpty()) {
                Element temporal = new Element("temporal", dctermsns);
                record.addContent(temporal);
                temporal.setText(theString);
            }
        }
    }

    //europeana:object

    if (doc.getField("lom.technical.duration") != null) {
        Element object = new Element("object", europeanans);
        record.addContent(object);
        String thumbString = doc.getField("lom.technical.duration").stringValue();
        if (thumbString != null)
            object.setText(thumbString);
    }

    //europeana:provider
    Element provider = new Element("provider", europeanans);
    record.addContent(provider);
    provider.setText("The Natural Europe Project");

    // europeana:type
    // When europeana:type is "TEXT" then a value for "dc:language must be provided".
    Element type_eu = new Element("type", europeanans);
    record.addContent(type_eu);

    if (doc.getField("lom.technical.format") != null) {
        String typeString = doc.getField("lom.technical.format").stringValue();
        type_eu.setText(typeString);

        if (typeString != null) {
            if (typeString.contains("image") || typeString.contains("jpeg") || typeString.contains("jpg"))
                type_eu.setText("IMAGE");
            else if (typeString.contains("video"))
                type_eu.setText("VIDEO");
            else if (typeString.contains("pdf") || typeString.contains("text"))
                type_eu.setText("TEXT");
        }
    }

    //europeana:rights
    Element rights = new Element("rights", europeanans);
    record.addContent(rights);

    if (doc.getField("lom.rights.copyrightandotherrestrictions.string") != null) {
        String rightsString = doc.getField("lom.rights.copyrightandotherrestrictions.string").stringValue();
        rightsString = "http://creativecommons.org/licenses/" + rightsString + "/3.0/";
        rights.setText(rightsString);
    }

    //europeana:dataProvider
    Element dataProvider = new Element("dataProvider", europeanans);
    record.addContent(dataProvider);

    if (doc.getField("lom.metametadata.identifier.catalog") != null) {
        String dataProviderString = doc.getField("lom.metametadata.identifier.catalog").stringValue();
        if (dataProviderString != null)
            dataProvider.setText(dataProviderString);
    }

    //europeana:isShownBy
    Element isShownBy = new Element("isShownBy", europeanans);
    record.addContent(isShownBy);
    if (doc.getField("lom.technical.location") != null) {
        String locationString = doc.getField("lom.technical.location").stringValue();
        if (locationString != null)
            isShownBy.setText(locationString);
    }
    //europeana:isShownAt

    String result = "";
    result = OaiUtils.parseLom2Xmlstring(record);
    return result;

}

From source file:org.bibsonomy.lucene.util.LuceneCommandLine.java

License:Open Source License

private void doSearching(IndexSearcher searcher, Sort sort, String searchTerms) {
    if (!"!quit".equals(searchTerms)) {
        long queryTimeMs = System.currentTimeMillis();
        Query searchQuery = parseSearchQuery(searchTerms);
        queryTimeMs = System.currentTimeMillis() - queryTimeMs;
        //------------------------------------------------------------
        // query the index
        //------------------------------------------------------------
        Document doc = null;
        try {//  ww w  . ja v a2 s .  c o  m
            TopDocs topDocs = null;
            topDocs = searcher.search(searchQuery, null, 100, sort);
            for (int i = 0; i < topDocs.totalHits; i++) {
                doc = searcher.doc(topDocs.scoreDocs[i].doc);
                System.out.println("Document[" + i + "]:");
                List<Fieldable> fields = doc.getFields();
                for (Fieldable field : fields) {
                    System.out.println("  " + field.name() + ":\t" + doc.getField(field.name()));
                }
            }
        } catch (Exception e) {
            log.error("Error reading index file (" + e.getMessage() + ")");
        } finally {
            try {
                searcher.close();
            } catch (IOException e) {
                log.error("Error closing index for searching", e);
            }
        }
        System.out.println("Query time: " + queryTimeMs);
    }
}

From source file:org.bidtime.lucene.ldbc.rs.handler.ColumnLSetDTOHandler.java

License:Apache License

/**
 * Returns one <code>ResultSet</code> column value as <code>Object</code>.
 * @param rs <code>ResultSet</code> to process.
 * @return <code>Object</code>, never <code>null</code>.
 *
 * @throws Exception if a database access error occurs
 * @throws ClassCastException if the class datatype does not match the column type
 *
 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
 *//*w w w . j a  v  a  2 s.co m*/
// We assume that the user has picked the correct type to match the column
// so getObject will return the appropriate type and the cast will succeed.
@SuppressWarnings("unchecked")
@Override
protected T handleRow(IndexSearcher searcher, ScoreDoc scoreDoc) throws Exception {
    Document doc = searcher.doc(scoreDoc.doc);
    IndexableField field = null;
    if (this.columnName == null) {
        field = doc.getFields().get(columnIndex);
    } else {
        field = doc.getField(columnName);
    }
    return (T) BeanProcessorEx.processColumn(field, this.type);
}

From source file:org.cee.store.lucene.LuceneStoreBase.java

License:Apache License

protected String getStringFieldOrNull(Document document, String fieldName) {
    IndexableField field = document.getField(fieldName);
    if (field == null) {
        return null;
    } else {//  w  w w  .j a  v a  2 s .c o  m
        return field.stringValue();
    }
}

From source file:org.chenillekit.lucene.services.LuceneIndexerServiceTest.java

License:Apache License

@BeforeClass
public void initialize_lucene_dictionary() {
    IndexerService service = registry.getService(IndexerService.class);
    String[] fileNames = new String[] { "airbag.txt", "consp.txt", "aliens.txt" };

    Document document = new Document();
    document.add(new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED));
    document.add(new Field("url", "", Field.Store.YES, Field.Index.NOT_ANALYZED));
    document.add(new Field("content", "", Field.Store.YES, Field.Index.ANALYZED));

    for (int i = 0; i < repeating; i++) {
        for (String fileName : fileNames) {
            Resource resource = new ClasspathResource(this.getClass().getClassLoader(), fileName);

            document.getField("id").setValue(fileName + "_" + String.valueOf(i));
            document.getField("url").setValue(resource.toURL().toString());
            document.getField("content").setValue(readFile(resource.toURL()));
            service.addDocument(document);
        }//w  w w  .  j  av  a  2 s .  c om
    }

    service.commit();
}

From source file:org.codehaus.mojo.nbm.PopulateRepositoryMojo.java

License:Apache License

private File createMavenProject(ModuleWrapper wrapper, List<ModuleWrapper> wrapperList,
        List<ExternalsWrapper> externalsList, IndexSearcher searcher) {
    Model mavenModel = new Model();

    mavenModel.setGroupId(wrapper.getGroup());
    mavenModel.setArtifactId(wrapper.getArtifact());
    mavenModel.setVersion(wrapper.getVersion());
    mavenModel.setPackaging("jar");
    mavenModel.setModelVersion("4.0.0");
    ExamineManifest man = wrapper.getModuleManifest();
    List<Dependency> deps = new ArrayList<Dependency>();
    if (!man.getDependencyTokens().isEmpty()) {
        for (String elem : man.getDependencyTokens()) {
            // create pseudo wrapper
            ModuleWrapper wr = new ModuleWrapper(elem);
            int index = wrapperList.indexOf(wr);
            if (index > -1) {
                wr = wrapperList.get(index);
                Dependency dep = new Dependency();
                dep.setArtifactId(wr.getArtifact());
                dep.setGroupId(wr.getGroup());
                dep.setVersion(wr.getVersion());
                dep.setType("jar");
                //we don't want the API modules to depend on non-api ones..
                // otherwise the transitive dependency mechanism pollutes your classpath..
                if (wrapper.getModuleManifest().hasPublicPackages()
                        && !wr.getModuleManifest().hasPublicPackages()) {
                    dep.setScope("runtime");
                }/* www  .ja  va2s .  c o m*/
                deps.add(dep);
            } else {
                getLog().warn("No module found for dependency '" + elem + "'");
            }
        }
    }
    //need some generic way to handle Classpath: items.
    //how to figure the right version?
    String cp = wrapper.getModuleManifest().getClasspath();
    if (!cp.isEmpty()) {
        StringTokenizer tok = new StringTokenizer(cp);
        while (tok.hasMoreTokens()) {
            String path = tok.nextToken();
            File f = new File(wrapper.getFile().getParentFile(), path);
            if (f.exists()) {
                FileInputStream fis = null;
                DigestOutputStream os = null;
                boolean added = false;
                if (searcher != null) {
                    try {
                        MessageDigest shaDig = MessageDigest.getInstance("SHA1");
                        fis = new FileInputStream(f);
                        os = new DigestOutputStream(new NullOutputStream(), shaDig);
                        IOUtil.copy(fis, os);
                        IOUtil.close(fis);
                        IOUtil.close(os);
                        String sha = encode(shaDig.digest());
                        TermQuery q = new TermQuery(new Term("1", sha));
                        Hits hits = searcher.search(q);
                        if (hits.length() == 1) {
                            Document doc = hits.doc(0);
                            Field idField = doc.getField("u");
                            if (idField != null) {
                                String id = idField.stringValue();
                                String[] splits = StringUtils.split(id, "|");
                                Dependency dep = new Dependency();
                                dep.setArtifactId(splits[1]);
                                dep.setGroupId(splits[0]);
                                dep.setVersion(splits[2]);
                                dep.setType("jar");
                                if (splits.length > 3 && !"NA".equals(splits[3])) {
                                    dep.setClassifier(splits[3]);
                                }
                                deps.add(dep);
                                added = true;
                            }
                        }
                    } catch (Exception x) {
                        x.printStackTrace();
                    }
                }
                if (!added) {
                    ExternalsWrapper ex = new ExternalsWrapper();
                    ex.setFile(f);
                    String artId = f.getName();
                    if (artId.endsWith(".jar")) {
                        artId = artId.substring(0, artId.length() - ".jar".length());
                    }
                    ex.setVersion(wrapper.getVersion());
                    ex.setArtifact(artId);
                    ex.setGroupid("org.netbeans.external");
                    externalsList.add(ex);
                    Dependency dep = new Dependency();
                    dep.setArtifactId(artId);
                    dep.setGroupId("org.netbeans.external");
                    dep.setVersion(wrapper.getVersion());
                    dep.setType("jar");
                    deps.add(dep);
                }
            }
        }
    }

    wrapper.deps = deps;
    mavenModel.setDependencies(deps);
    FileWriter writer = null;
    File fil = null;
    try {
        MavenXpp3Writer xpp = new MavenXpp3Writer();
        fil = File.createTempFile("maven", ".pom");
        fil.deleteOnExit();
        writer = new FileWriter(fil);
        xpp.write(writer, mavenModel);
    } catch (IOException ex) {
        ex.printStackTrace();

    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException io) {
                io.printStackTrace();
            }
        }
    }
    return fil;
}

From source file:org.compass.core.lucene.LuceneResource.java

License:Apache License

public LuceneResource(String alias, Document document, int docNum,
        LuceneSearchEngineFactory searchEngineFactory) {
    this.searchEngineFactory = searchEngineFactory;
    String aliasProperty = searchEngineFactory.getAliasProperty();
    this.docNum = docNum;
    if (alias != null) {
        this.alias = alias;
        Field aliasField = new Field(aliasProperty, alias, Field.Store.YES, Field.Index.NOT_ANALYZED);
        aliasField.setOmitNorms(true);//from  w  ww.ja  v a 2  s.c o m
        properties.add(new LuceneProperty(aliasField));
    } else {
        Fieldable aliasField = document.getField(aliasProperty);
        if (aliasField != null) {
            properties.add(new LuceneProperty(aliasField));
            this.alias = aliasField.stringValue();
        }
    }

    verifyResourceMapping();

    List fields = document.getFields();
    for (Iterator fieldsIt = fields.iterator(); fieldsIt.hasNext();) {
        Fieldable field = (Fieldable) fieldsIt.next();
        if (field.name().equals(aliasProperty)) {
            continue;
        }
        LuceneProperty lProperty = new LuceneProperty(field);
        lProperty.setPropertyMapping(resourceMapping.getResourcePropertyMapping(field.name()));
        properties.add(lProperty);
    }
}

From source file:org.controlhaus.apache.lucene.pojo.LucenePojo.java

License:Apache License

private void addFields(String[] returnFieldNames, Document doc, LuceneHit hit) {
    for (int j = 0; j < returnFieldNames.length; j++) {
        Field field = doc.getField(returnFieldNames[j]);
        hit.addField(new LuceneField(field.name(), field.stringValue()));
    }/*ww  w  . ja  v a2s  . c o m*/
}