Search in sources :

Example 56 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testImplicitlySetDefaultScriptLang.

public void testImplicitlySetDefaultScriptLang() throws Exception {
    addQueryMapping();
    XContentBuilder query = jsonBuilder();
    query.startObject();
    query.startObject("script");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("inline", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    ParsedDocument doc = mapperService.documentMapper(typeName).parse("test", typeName, "1", XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(query.string()), query.contentType()).endObject().bytes());
    BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    Map<String, Object> parsedQuery = XContentHelper.convertToMap(new BytesArray(querySource), true).v2();
    assertEquals(Script.DEFAULT_SCRIPT_LANG, XContentMapValues.extractValue("script.script.lang", parsedQuery));
    query = jsonBuilder();
    query.startObject();
    query.startObject("function_score");
    query.startArray("functions");
    query.startObject();
    query.startObject("script_score");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("inline", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    query.endArray();
    query.endObject();
    query.endObject();
    doc = mapperService.documentMapper(typeName).parse("test", typeName, "1", XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(query.string()), query.contentType()).endObject().bytes());
    querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    parsedQuery = XContentHelper.convertToMap(new BytesArray(querySource), true).v2();
    assertEquals(Script.DEFAULT_SCRIPT_LANG, ((List) XContentMapValues.extractValue("function_score.functions.script_score.script.lang", parsedQuery)).get(0));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 57 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_termsQuery.

public void testExtractQueryMetadata_termsQuery() {
    TermInSetQuery termsQuery = new TermInSetQuery("_field", new BytesRef("_term1"), new BytesRef("_term2"));
    Result result = analyze(termsQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(2));
    assertThat(terms.get(0).field(), equalTo("_field"));
    assertThat(terms.get(0).text(), equalTo("_term1"));
    assertThat(terms.get(1).field(), equalTo("_field"));
    assertThat(terms.get(1).text(), equalTo("_term2"));
}
Also used : TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) BytesRef(org.apache.lucene.util.BytesRef) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 58 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class ByteBufStreamInput method readBytesRef.

@Override
public BytesRef readBytesRef(int length) throws IOException {
    if (!buffer.hasArray()) {
        return super.readBytesRef(length);
    }
    BytesRef bytesRef = new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length);
    buffer.skipBytes(length);
    return bytesRef;
}
Also used : BytesRef(org.apache.lucene.util.BytesRef)

Example 59 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class ByteBufBytesReferenceTests method testSliceOnAdvancedBuffer.

public void testSliceOnAdvancedBuffer() throws IOException {
    BytesReference bytesReference = newBytesReference(randomIntBetween(10, 3 * PAGE_SIZE));
    BytesRef bytesRef = bytesReference.toBytesRef();
    ByteBuf channelBuffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
    int numBytesToRead = randomIntBetween(1, 5);
    for (int i = 0; i < numBytesToRead; i++) {
        channelBuffer.readByte();
    }
    BytesReference other = Netty4Utils.toBytesReference(channelBuffer);
    BytesReference slice = bytesReference.slice(numBytesToRead, bytesReference.length() - numBytesToRead);
    assertEquals(other, slice);
    assertEquals(other.slice(3, 1), slice.slice(3, 1));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ByteBuf(io.netty.buffer.ByteBuf) BytesRef(org.apache.lucene.util.BytesRef)

Example 60 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class Murmur3FieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    final Object value;
    if (context.externalValueSet()) {
        value = context.externalValue();
    } else {
        value = context.parser().textOrNull();
    }
    if (value != null) {
        final BytesRef bytes = new BytesRef(value.toString());
        final long hash = MurmurHash3.hash128(bytes.bytes, bytes.offset, bytes.length, 0, new MurmurHash3.Hash128()).h1;
        fields.add(new SortedNumericDocValuesField(fieldType().name(), hash));
        if (fieldType().stored()) {
            fields.add(new StoredField(name(), hash));
        }
    }
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRef (org.apache.lucene.util.BytesRef)1449 Document (org.apache.lucene.document.Document)410 Directory (org.apache.lucene.store.Directory)370 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)266 ArrayList (java.util.ArrayList)186 Test (org.junit.Test)182 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)164 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)152 Term (org.apache.lucene.index.Term)124 Analyzer (org.apache.lucene.analysis.Analyzer)121 IndexReader (org.apache.lucene.index.IndexReader)121 TermsEnum (org.apache.lucene.index.TermsEnum)116 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)110 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)105 IOException (java.io.IOException)104 Field (org.apache.lucene.document.Field)101 StringField (org.apache.lucene.document.StringField)101 CrateUnitTest (io.crate.test.integration.CrateUnitTest)95 TextField (org.apache.lucene.document.TextField)95 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)87