Search in sources :

Example 1 with LeafFieldData

use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.

the class LeafDocLookupTests method createFieldData.

private IndexFieldData<?> createFieldData(ScriptDocValues scriptDocValues) {
    LeafFieldData leafFieldData = mock(LeafFieldData.class);
    doReturn(scriptDocValues).when(leafFieldData).getScriptValues();
    IndexFieldData<?> fieldData = mock(IndexFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    doReturn(leafFieldData).when(fieldData).load(any());
    return fieldData;
}
Also used : LeafFieldData(org.opensearch.index.fielddata.LeafFieldData)

Example 2 with LeafFieldData

use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.

the class RandomScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
    final SortedBinaryDocValues values;
    if (fieldData != null) {
        LeafFieldData leafData = fieldData.load(ctx);
        values = leafData.getBytesValues();
        if (values == null)
            throw new NullPointerException("failed to get fielddata");
    } else {
        values = null;
    }
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) throws IOException {
            int hash;
            if (values == null) {
                hash = BitMixer.mix(ctx.docBase + docId, saltedSeed);
            } else if (values.advanceExact(docId)) {
                hash = StringHelper.murmurhash3_x86_32(values.nextValue(), saltedSeed);
            } else {
                // field has no value
                hash = saltedSeed;
            }
            // only use the lower 24 bits to construct a float from 0.0-1.0
            return (hash & 0x00FFFFFF) / (float) (1 << 24);
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
            String field = fieldData == null ? null : fieldData.getFieldName();
            return Explanation.match((float) score(docId, subQueryScore.getValue().floatValue()), "random score function (seed: " + originalSeed + ", field: " + field + Functions.nameOrEmptyArg(functionName) + ")");
        }
    };
}
Also used : Explanation(org.apache.lucene.search.Explanation) LeafFieldData(org.opensearch.index.fielddata.LeafFieldData) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 3 with LeafFieldData

use of org.opensearch.index.fielddata.LeafFieldData in project OpenSearch by opensearch-project.

the class QueryShardContextTests method mockFieldType.

private static MappedFieldType mockFieldType(String fieldName, BiFunction<LeafSearchLookup, Integer, String> runtimeDocValues) {
    MappedFieldType fieldType = mock(MappedFieldType.class);
    when(fieldType.name()).thenReturn(fieldName);
    when(fieldType.fielddataBuilder(any(), any())).thenAnswer(builderInv -> {
        @SuppressWarnings("unchecked") Supplier<SearchLookup> searchLookup = ((Supplier<SearchLookup>) builderInv.getArguments()[1]);
        IndexFieldData<?> indexFieldData = mock(IndexFieldData.class);
        when(indexFieldData.load(any())).thenAnswer(loadArgs -> {
            LeafReaderContext leafReaderContext = (LeafReaderContext) loadArgs.getArguments()[0];
            LeafFieldData leafFieldData = mock(LeafFieldData.class);
            when(leafFieldData.getScriptValues()).thenAnswer(scriptValuesArgs -> new ScriptDocValues<String>() {

                String value;

                @Override
                public int size() {
                    return 1;
                }

                @Override
                public String get(int index) {
                    assert index == 0;
                    return value;
                }

                @Override
                public void setNextDocId(int docId) {
                    assert docId >= 0;
                    LeafSearchLookup leafLookup = searchLookup.get().getLeafSearchLookup(leafReaderContext);
                    leafLookup.setDocument(docId);
                    value = runtimeDocValues.apply(leafLookup, docId);
                }
            });
            return leafFieldData;
        });
        IndexFieldData.Builder builder = mock(IndexFieldData.Builder.class);
        when(builder.build(any(), any())).thenAnswer(buildInv -> indexFieldData);
        return builder;
    });
    return fieldType;
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) LeafFieldData(org.opensearch.index.fielddata.LeafFieldData) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Aggregations

LeafFieldData (org.opensearch.index.fielddata.LeafFieldData)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Explanation (org.apache.lucene.search.Explanation)1 IndexFieldData (org.opensearch.index.fielddata.IndexFieldData)1 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1 LeafSearchLookup (org.opensearch.search.lookup.LeafSearchLookup)1 SearchLookup (org.opensearch.search.lookup.SearchLookup)1