use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class EnumField method createFields.
/**
* {@inheritDoc}
*/
@Override
public List<IndexableField> createFields(SchemaField sf, Object value) {
if (sf.hasDocValues()) {
List<IndexableField> fields = new ArrayList<>();
final IndexableField field = createField(sf, value);
fields.add(field);
if (sf.multiValued()) {
BytesRefBuilder bytes = new BytesRefBuilder();
readableToIndexed(stringValueToIntValue(value.toString()).toString(), bytes);
fields.add(new SortedSetDocValuesField(sf.getName(), bytes.toBytesRef()));
} else {
final long bits = field.numericValue().intValue();
fields.add(new NumericDocValuesField(sf.getName(), bits));
}
return fields;
} else {
return Collections.singletonList(createField(sf, value));
}
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class EnumField method readableToIndexed.
/**
* {@inheritDoc}
*/
@Override
public String readableToIndexed(String val) {
if (val == null)
return null;
final BytesRefBuilder bytes = new BytesRefBuilder();
readableToIndexed(val, bytes);
return bytes.get().utf8ToString();
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class FieldType method getFieldQuery.
/**
* Returns a Query instance for doing searches against a field.
* @param parser The {@link org.apache.solr.search.QParser} calling the method
* @param field The {@link org.apache.solr.schema.SchemaField} of the field to search
* @param externalVal The String representation of the value to search
* @return The {@link org.apache.lucene.search.Query} instance. This implementation returns a {@link org.apache.lucene.search.TermQuery} but overriding queries may not
*
*/
public Query getFieldQuery(QParser parser, SchemaField field, String externalVal) {
BytesRefBuilder br = new BytesRefBuilder();
readableToIndexed(externalVal, br);
if (field.hasDocValues() && !field.indexed()) {
// match-only
return getRangeQuery(parser, field, externalVal, externalVal, true, true);
} else {
return new TermQuery(new Term(field.getName(), br));
}
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class TestFSTs method testExpandedCloseToRoot.
/**
* Test state expansion (array format) on close-to-root states. Creates
* synthetic input that has one expanded state on each level.
*
* @see <a href="https://issues.apache.org/jira/browse/LUCENE-2933">LUCENE-2933</a>
*/
public void testExpandedCloseToRoot() throws Exception {
class SyntheticData {
FST<Object> compile(String[] lines) throws IOException {
final NoOutputs outputs = NoOutputs.getSingleton();
final Object nothing = outputs.getNoOutput();
final Builder<Object> b = new Builder<>(FST.INPUT_TYPE.BYTE1, outputs);
int line = 0;
final BytesRefBuilder term = new BytesRefBuilder();
final IntsRefBuilder scratchIntsRef = new IntsRefBuilder();
while (line < lines.length) {
String w = lines[line++];
if (w == null) {
break;
}
term.copyChars(w);
b.add(Util.toIntsRef(term.get(), scratchIntsRef), nothing);
}
return b.finish();
}
void generate(ArrayList<String> out, StringBuilder b, char from, char to, int depth) {
if (depth == 0 || from == to) {
String seq = b.toString() + "_" + out.size() + "_end";
out.add(seq);
} else {
for (char c = from; c <= to; c++) {
b.append(c);
generate(out, b, from, c == to ? to : from, depth - 1);
b.deleteCharAt(b.length() - 1);
}
}
}
public int verifyStateAndBelow(FST<Object> fst, Arc<Object> arc, int depth) throws IOException {
if (FST.targetHasArcs(arc)) {
int childCount = 0;
BytesReader fstReader = fst.getBytesReader();
for (arc = fst.readFirstTargetArc(arc, arc, fstReader); ; arc = fst.readNextArc(arc, fstReader), childCount++) {
boolean expanded = fst.isExpandedTarget(arc, fstReader);
int children = verifyStateAndBelow(fst, new FST.Arc<>().copyFrom(arc), depth + 1);
assertEquals(expanded, (depth <= FST.FIXED_ARRAY_SHALLOW_DISTANCE && children >= FST.FIXED_ARRAY_NUM_ARCS_SHALLOW) || children >= FST.FIXED_ARRAY_NUM_ARCS_DEEP);
if (arc.isLast())
break;
}
return childCount;
}
return 0;
}
}
// Sanity check.
assertTrue(FST.FIXED_ARRAY_NUM_ARCS_SHALLOW < FST.FIXED_ARRAY_NUM_ARCS_DEEP);
assertTrue(FST.FIXED_ARRAY_SHALLOW_DISTANCE >= 0);
SyntheticData s = new SyntheticData();
ArrayList<String> out = new ArrayList<>();
StringBuilder b = new StringBuilder();
s.generate(out, b, 'a', 'i', 10);
String[] input = out.toArray(new String[out.size()]);
Arrays.sort(input);
FST<Object> fst = s.compile(input);
FST.Arc<Object> arc = fst.getFirstArc(new FST.Arc<>());
s.verifyStateAndBelow(fst, arc, 1);
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class TestCompiledAutomaton method testFloor.
private void testFloor(CompiledAutomaton c, String input, String expected) {
final BytesRef b = new BytesRef(input);
final BytesRef result = c.floor(b, new BytesRefBuilder());
if (expected == null) {
assertNull(result);
} else {
assertNotNull(result);
assertEquals("actual=" + result.utf8ToString() + " vs expected=" + expected + " (input=" + input + ")", result, new BytesRef(expected));
}
}
Aggregations