Search in sources :

Example 41 with BytesRefBuilder

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));
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ArrayList(java.util.ArrayList) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField)

Example 42 with BytesRefBuilder

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();
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder)

Example 43 with BytesRefBuilder

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));
    }
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Term(org.apache.lucene.index.Term)

Example 44 with BytesRefBuilder

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);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) IntsRefBuilder(org.apache.lucene.util.IntsRefBuilder) ArrayList(java.util.ArrayList) FSTTester.simpleRandomString(org.apache.lucene.util.fst.FSTTester.simpleRandomString) FSTTester.getRandomString(org.apache.lucene.util.fst.FSTTester.getRandomString) IntsRefBuilder(org.apache.lucene.util.IntsRefBuilder) BytesReader(org.apache.lucene.util.fst.FST.BytesReader) Arc(org.apache.lucene.util.fst.FST.Arc)

Example 45 with BytesRefBuilder

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));
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)141 BytesRef (org.apache.lucene.util.BytesRef)73 ArrayList (java.util.ArrayList)20 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 Term (org.apache.lucene.index.Term)14 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)14 FieldType (org.apache.solr.schema.FieldType)13 IndexInput (org.apache.lucene.store.IndexInput)12 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 IntsRef (org.apache.lucene.util.IntsRef)10 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)9 SchemaField (org.apache.solr.schema.SchemaField)9 ParseException (java.text.ParseException)8 IntsRefBuilder (org.apache.lucene.util.IntsRefBuilder)8 DecimalFormat (java.text.DecimalFormat)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Directory (org.apache.lucene.store.Directory)7 IndexOutput (org.apache.lucene.store.IndexOutput)6