Search in sources :

Example 31 with TextField

use of org.apache.lucene.document.TextField in project OpenGrok by OpenGrok.

the class UuencodeAnalyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
    //this is to explicitely use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search 
    TextField full = new TextField(QueryBuilder.FULL, SymbolTokenizer);
    this.SymbolTokenizer.setReader(getReader(src.getStream()));
    doc.add(full);
    if (xrefOut != null) {
        try (Reader in = getReader(src.getStream())) {
            writeXref(in, xrefOut);
        }
    }
}
Also used : TextField(org.apache.lucene.document.TextField) Reader(java.io.Reader)

Example 32 with TextField

use of org.apache.lucene.document.TextField in project orientdb by orientechnologies.

the class LuceneVsLuceneTest method testLuceneVsLucene.

@Test
public void testLuceneVsLucene() throws IOException, ParseException {
    for (ODocument oDocument : db.browseClass("Song")) {
        String title = oDocument.field("title");
        if (title != null) {
            Document d = new Document();
            d.add(new TextField("title", title, Field.Store.YES));
            d.add(new TextField("Song.title", title, Field.Store.YES));
            indexWriter.addDocument(d);
        }
    }
    indexWriter.commit();
    indexWriter.close();
    IndexReader reader = DirectoryReader.open(getDirectory());
    assertThat(reader.numDocs()).isEqualTo(Long.valueOf(db.countClass("Song")).intValue());
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new MultiFieldQueryParser(new String[] { "title" }, analyzer).parse("down the");
    final TopDocs docs = searcher.search(query, Integer.MAX_VALUE);
    ScoreDoc[] hits = docs.scoreDocs;
    List<ODocument> oDocs = db.query(new OSQLSynchQuery<ODocument>("select *,$score from Song where title LUCENE \"down the\""));
    Assert.assertEquals(oDocs.size(), hits.length);
    int i = 0;
    for (ScoreDoc hit : hits) {
        Assert.assertEquals(oDocs.get(i).field("$score"), hit.score);
        i++;
    }
    reader.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Document(org.apache.lucene.document.Document) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 33 with TextField

use of org.apache.lucene.document.TextField in project querydsl by querydsl.

the class LuceneQueryTest method createDocument.

private Document createDocument(final String docTitle, final String docAuthor, final String docText, final int docYear, final double docGross) {
    Document doc = new Document();
    // Reusing field for performance
    if (titleField == null) {
        titleField = new TextField("title", docTitle, Store.YES);
        doc.add(titleField);
        titleSortedField = new SortedDocValuesField("title", new BytesRef(docTitle));
        doc.add(titleSortedField);
    } else {
        titleField.setStringValue(docTitle);
        titleSortedField.setBytesValue(new BytesRef(docTitle));
        doc.add(titleField);
        doc.add(titleSortedField);
    }
    if (authorField == null) {
        authorField = new TextField("author", docAuthor, Store.YES);
        doc.add(authorField);
        authorSortedField = new SortedDocValuesField("author", new BytesRef(docAuthor));
        doc.add(authorSortedField);
    } else {
        authorField.setStringValue(docAuthor);
        authorSortedField.setBytesValue(new BytesRef(docAuthor));
        doc.add(authorField);
        doc.add(authorSortedField);
    }
    if (textField == null) {
        textField = new TextField("text", docText, Store.YES);
        doc.add(textField);
        textSortedField = new SortedDocValuesField("text", new BytesRef(docText));
        doc.add(textSortedField);
    } else {
        textField.setStringValue(docText);
        textSortedField.setBytesValue(new BytesRef(docText));
        doc.add(textField);
        doc.add(textSortedField);
    }
    if (yearField == null) {
        yearField = new IntField("year", docYear, Store.YES);
        doc.add(yearField);
        yearSortedField = new NumericDocValuesField("year", docYear);
        doc.add(yearSortedField);
    } else {
        yearField.setIntValue(docYear);
        yearSortedField.setLongValue(docYear);
        doc.add(yearField);
        doc.add(yearSortedField);
    }
    if (grossField == null) {
        grossField = new DoubleField("gross", docGross, Store.YES);
        doc.add(grossField);
        grossSortedField = new DoubleDocValuesField("gross", docGross);
        doc.add(grossSortedField);
    } else {
        grossField.setDoubleValue(docGross);
        grossSortedField.setDoubleValue(docGross);
        doc.add(grossField);
        doc.add(grossSortedField);
    }
    return doc;
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) TextField(org.apache.lucene.document.TextField) IntField(org.apache.lucene.document.IntField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) DoubleField(org.apache.lucene.document.DoubleField)

Example 34 with TextField

use of org.apache.lucene.document.TextField in project languagetool by languagetool-org.

the class SimilarWordFinder method createIndex.

private void createIndex(List<String> words, File indexDir) throws IOException {
    FSDirectory dir = FSDirectory.open(indexDir.toPath());
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    System.out.println("Creating index...");
    int docs = 0;
    try (IndexWriter writer = new IndexWriter(dir, indexWriterConfig)) {
        for (String word : words) {
            Document doc = new Document();
            doc.add(new TextField("word", word, Field.Store.YES));
            writer.addDocument(doc);
            docs++;
        }
    }
    System.out.println("Index created: " + docs + " docs");
}
Also used : StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextField(org.apache.lucene.document.TextField) FSDirectory(org.apache.lucene.store.FSDirectory) Document(org.apache.lucene.document.Document)

Example 35 with TextField

use of org.apache.lucene.document.TextField in project languagetool by languagetool-org.

the class LuceneSimpleIndexCreator method main.

public static void main(String[] args) throws IOException {
    IndexWriterConfig conf = new IndexWriterConfig(new KeywordAnalyzer());
    try (IndexWriter iw1 = new IndexWriter(FSDirectory.open(new File("/tmp/1grams").toPath()), conf)) {
        addDoc(iw1, "the", 55);
        addDoc(iw1, "nice", 10);
        addDoc(iw1, "building", 1);
        Document document = new Document();
        document.add(new TextField("totalTokenCount", String.valueOf(3), Field.Store.YES));
        iw1.addDocument(document);
    }
    IndexWriterConfig conf2 = new IndexWriterConfig(new KeywordAnalyzer());
    try (IndexWriter iw2 = new IndexWriter(FSDirectory.open(new File("/tmp/2grams").toPath()), conf2)) {
        addDoc(iw2, "the nice", 3);
        addDoc(iw2, "nice building", 2);
    }
    IndexWriterConfig conf3 = new IndexWriterConfig(new KeywordAnalyzer());
    try (IndexWriter iw3 = new IndexWriter(FSDirectory.open(new File("/tmp/3grams").toPath()), conf3)) {
        addDoc(iw3, "the nice building", 1);
    }
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) File(java.io.File) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

TextField (org.apache.lucene.document.TextField)192 Document (org.apache.lucene.document.Document)171 Directory (org.apache.lucene.store.Directory)99 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)61 Term (org.apache.lucene.index.Term)61 IndexWriter (org.apache.lucene.index.IndexWriter)58 IndexSearcher (org.apache.lucene.search.IndexSearcher)55 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)52 Field (org.apache.lucene.document.Field)50 StringField (org.apache.lucene.document.StringField)48 BytesRef (org.apache.lucene.util.BytesRef)48 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)44 IndexReader (org.apache.lucene.index.IndexReader)43 TermQuery (org.apache.lucene.search.TermQuery)41 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)31 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)30 TopDocs (org.apache.lucene.search.TopDocs)29 RAMDirectory (org.apache.lucene.store.RAMDirectory)29 FieldType (org.apache.lucene.document.FieldType)23 Query (org.apache.lucene.search.Query)23