/
StoredFieldCommand.java
103 lines (78 loc) · 2.42 KB
/
StoredFieldCommand.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.senseidb.clue.commands;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.BytesRef;
import com.senseidb.clue.ClueContext;
public class StoredFieldCommand extends ClueCommand {
public StoredFieldCommand(ClueContext ctx) {
super(ctx);
}
@Override
public String getName() {
return "stored";
}
@Override
public String help() {
return "displays stored data for a given field";
}
@Override
public void execute(String[] args, PrintStream out) throws Exception {
if (args.length != 2) {
out.println("usage: field doc");
return;
}
String field = args[0];
int doc = Integer.parseInt(args[1]);
IndexReader reader = ctx.getIndexReader();
List<AtomicReaderContext> leaves = reader.leaves();
boolean found = false;
boolean stored = false;
for (AtomicReaderContext ctx : leaves) {
AtomicReader atomicReader = ctx.reader();
FieldInfo finfo = atomicReader.getFieldInfos().fieldInfo(field);
if (finfo == null) continue;
stored = true;
int docID = doc - ctx.docBase;
if (docID >= 0) {
Document storedData = atomicReader.document(docID, new HashSet<String>(Arrays.asList(field)));
if (storedData == null) continue;
String strData = storedData.get(field);
if (strData != null) {
out.println(strData);
found = true;
break;
}
BytesRef bytesRef = storedData.getBinaryValue(field);
if (bytesRef != null) {
out.println(bytesRef);
found = true;
break;
}
BytesRef[] dataArray = storedData.getBinaryValues(field);
if (dataArray == null || dataArray.length == 0) {
continue;
}
for (BytesRef data : dataArray){
out.println(data);
}
found = true;
break;
}
}
if (!stored) {
out.println("stored data is not available for field: "+field);
return;
}
if (!found) {
out.println(doc+" not found");
return;
}
}
}