Search in sources :

Example 1 with TermDocs

use of gate.creole.annic.apache.lucene.index.TermDocs in project gate-core by GateNLP.

the class FieldCacheImpl method getStringIndex.

// inherit javadocs
@Override
public StringIndex getStringIndex(IndexReader reader, String field) throws IOException {
    field = field.intern();
    Object ret = lookup(reader, field, STRING_INDEX);
    if (ret == null) {
        final int[] retArray = new int[reader.maxDoc()];
        String[] mterms = new String[reader.maxDoc() + 1];
        if (retArray.length > 0) {
            TermDocs termDocs = reader.termDocs();
            TermEnum termEnum = reader.terms(new Term(field, ""));
            // current term number
            int t = 0;
            // an entry for documents that have no terms in this field
            // should a document with no terms be at top or bottom?
            // this puts them at the top - if it is changed, FieldDocSortedHitQueue
            // needs to change as well.
            mterms[t++] = null;
            try {
                if (termEnum.term() == null) {
                    throw new RuntimeException("no terms in field " + field);
                }
                do {
                    Term term = termEnum.term();
                    if (term.field() != field)
                        break;
                    // we expect that there is at most one term per document
                    if (t >= mterms.length)
                        throw new RuntimeException("there are more terms than documents in field \"" + field + "\"");
                    mterms[t] = term.text();
                    termDocs.seek(termEnum);
                    while (termDocs.next()) {
                        retArray[termDocs.doc()] = t;
                    }
                    t++;
                } while (termEnum.next());
            } finally {
                termDocs.close();
                termEnum.close();
            }
            if (t == 0) {
                // if there are no terms, make the term array
                // have a single null entry
                mterms = new String[1];
            } else if (t < mterms.length) {
                // if there are less terms than documents,
                // trim off the dead array space
                String[] terms = new String[t];
                System.arraycopy(mterms, 0, terms, 0, t);
                mterms = terms;
            }
        }
        StringIndex value = new StringIndex(retArray, mterms);
        store(reader, field, STRING_INDEX, value);
        return value;
    }
    return (StringIndex) ret;
}
Also used : TermDocs(gate.creole.annic.apache.lucene.index.TermDocs) Term(gate.creole.annic.apache.lucene.index.Term) TermEnum(gate.creole.annic.apache.lucene.index.TermEnum)

Example 2 with TermDocs

use of gate.creole.annic.apache.lucene.index.TermDocs in project gate-core by GateNLP.

the class FieldCacheImpl method getInts.

// inherit javadocs
@Override
public int[] getInts(IndexReader reader, String field) throws IOException {
    field = field.intern();
    Object ret = lookup(reader, field, SortField.INT);
    if (ret == null) {
        final int[] retArray = new int[reader.maxDoc()];
        if (retArray.length > 0) {
            TermDocs termDocs = reader.termDocs();
            TermEnum termEnum = reader.terms(new Term(field, ""));
            try {
                if (termEnum.term() == null) {
                    throw new RuntimeException("no terms in field " + field);
                }
                do {
                    Term term = termEnum.term();
                    if (term.field() != field)
                        break;
                    int termval = Integer.parseInt(term.text());
                    termDocs.seek(termEnum);
                    while (termDocs.next()) {
                        retArray[termDocs.doc()] = termval;
                    }
                } while (termEnum.next());
            } finally {
                termDocs.close();
                termEnum.close();
            }
        }
        store(reader, field, SortField.INT, retArray);
        return retArray;
    }
    return (int[]) ret;
}
Also used : TermDocs(gate.creole.annic.apache.lucene.index.TermDocs) Term(gate.creole.annic.apache.lucene.index.Term) TermEnum(gate.creole.annic.apache.lucene.index.TermEnum)

Example 3 with TermDocs

use of gate.creole.annic.apache.lucene.index.TermDocs in project gate-core by GateNLP.

the class FieldCacheImpl method getCustom.

// inherit javadocs
@Override
public Comparable[] getCustom(IndexReader reader, String field, SortComparator comparator) throws IOException {
    field = field.intern();
    Object ret = lookup(reader, field, comparator);
    if (ret == null) {
        final Comparable[] retArray = new Comparable[reader.maxDoc()];
        if (retArray.length > 0) {
            TermDocs termDocs = reader.termDocs();
            TermEnum termEnum = reader.terms(new Term(field, ""));
            try {
                if (termEnum.term() == null) {
                    throw new RuntimeException("no terms in field " + field);
                }
                do {
                    Term term = termEnum.term();
                    if (term.field() != field)
                        break;
                    Comparable termval = comparator.getComparable(term.text());
                    termDocs.seek(termEnum);
                    while (termDocs.next()) {
                        retArray[termDocs.doc()] = termval;
                    }
                } while (termEnum.next());
            } finally {
                termDocs.close();
                termEnum.close();
            }
        }
        store(reader, field, SortField.CUSTOM, retArray);
        return retArray;
    }
    return (String[]) ret;
}
Also used : TermDocs(gate.creole.annic.apache.lucene.index.TermDocs) Term(gate.creole.annic.apache.lucene.index.Term) TermEnum(gate.creole.annic.apache.lucene.index.TermEnum)

Example 4 with TermDocs

use of gate.creole.annic.apache.lucene.index.TermDocs in project gate-core by GateNLP.

the class FieldCacheImpl method getFloats.

// inherit javadocs
@Override
public float[] getFloats(IndexReader reader, String field) throws IOException {
    field = field.intern();
    Object ret = lookup(reader, field, SortField.FLOAT);
    if (ret == null) {
        final float[] retArray = new float[reader.maxDoc()];
        if (retArray.length > 0) {
            TermDocs termDocs = reader.termDocs();
            TermEnum termEnum = reader.terms(new Term(field, ""));
            try {
                if (termEnum.term() == null) {
                    throw new RuntimeException("no terms in field " + field);
                }
                do {
                    Term term = termEnum.term();
                    if (term.field() != field)
                        break;
                    float termval = Float.parseFloat(term.text());
                    termDocs.seek(termEnum);
                    while (termDocs.next()) {
                        retArray[termDocs.doc()] = termval;
                    }
                } while (termEnum.next());
            } finally {
                termDocs.close();
                termEnum.close();
            }
        }
        store(reader, field, SortField.FLOAT, retArray);
        return retArray;
    }
    return (float[]) ret;
}
Also used : TermDocs(gate.creole.annic.apache.lucene.index.TermDocs) Term(gate.creole.annic.apache.lucene.index.Term) TermEnum(gate.creole.annic.apache.lucene.index.TermEnum)

Example 5 with TermDocs

use of gate.creole.annic.apache.lucene.index.TermDocs in project gate-core by GateNLP.

the class FieldCacheImpl method getStrings.

// inherit javadocs
@Override
public String[] getStrings(IndexReader reader, String field) throws IOException {
    field = field.intern();
    Object ret = lookup(reader, field, SortField.STRING);
    if (ret == null) {
        final String[] retArray = new String[reader.maxDoc()];
        if (retArray.length > 0) {
            TermDocs termDocs = reader.termDocs();
            TermEnum termEnum = reader.terms(new Term(field, ""));
            try {
                if (termEnum.term() == null) {
                    throw new RuntimeException("no terms in field " + field);
                }
                do {
                    Term term = termEnum.term();
                    if (term.field() != field)
                        break;
                    String termval = term.text();
                    termDocs.seek(termEnum);
                    while (termDocs.next()) {
                        retArray[termDocs.doc()] = termval;
                    }
                } while (termEnum.next());
            } finally {
                termDocs.close();
                termEnum.close();
            }
        }
        store(reader, field, SortField.STRING, retArray);
        return retArray;
    }
    return (String[]) ret;
}
Also used : TermDocs(gate.creole.annic.apache.lucene.index.TermDocs) Term(gate.creole.annic.apache.lucene.index.Term) TermEnum(gate.creole.annic.apache.lucene.index.TermEnum)

Aggregations

Term (gate.creole.annic.apache.lucene.index.Term)5 TermDocs (gate.creole.annic.apache.lucene.index.TermDocs)5 TermEnum (gate.creole.annic.apache.lucene.index.TermEnum)5