-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocLengthStore.java
44 lines (39 loc) · 1.39 KB
/
DocLengthStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (c) 2016, Carnegie Mellon University. All Rights Reserved.
*/
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.MultiDocValues;
import org.apache.lucene.index.MultiFields;
/**
* DocLengthStore is used to access the document lengths of indexed docs.
*/
public class DocLengthStore {
private IndexReader reader;
private Map<String, NumericDocValues> values = new HashMap<String, NumericDocValues>();
/**
* @param reader IndexReader object created in {@link Idx}.
* @throws IOException Error accessing the Lucene index.
*/
public DocLengthStore(IndexReader reader) throws IOException {
this.reader = reader;
for (String field : MultiFields.getIndexedFields(reader)) {
this.values.put(field, MultiDocValues.getNormValues(reader, field));
}
}
/**
* Returns the length of the specified field in the specified document.
*
* @param fieldname Name of field to access lengths. "body" is the default
* field.
* @param docid The internal docid in the lucene index.
* @return long The length of the field.
* @throws IOException Error accessing the Lucene index.
*/
public long getDocLength(String fieldname, int docid) throws IOException {
return values.get(fieldname).get(docid);
}
}