All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.search.suggest.DocumentValueSourceDictionary Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
package org.apache.lucene.search.suggest;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.util.BytesRefIterator;


/**
 * 

* Dictionary with terms and optionally payload information * taken from stored fields in a Lucene index. Similar to * {@link DocumentDictionary}, except it obtains the weight * of the terms in a document based on a {@link ValueSource}. *

* NOTE: *
    *
  • * The term and (optionally) payload fields have to be * stored *
  • *
  • * if the term or (optionally) payload fields supplied * do not have a value for a document, then the document is * rejected by the dictionary *
  • *
*

* In practice the {@link ValueSource} will likely be obtained * using the lucene expression module. The following example shows * how to create a {@link ValueSource} from a simple addition of two * fields: * * Expression expression = JavascriptCompiler.compile("f1 + f2"); * SimpleBindings bindings = new SimpleBindings(); * bindings.add(new SortField("f1", SortField.Type.LONG)); * bindings.add(new SortField("f2", SortField.Type.LONG)); * ValueSource valueSource = expression.getValueSource(bindings); * *

* */ public class DocumentValueSourceDictionary extends DocumentDictionary { private final ValueSource weightsValueSource; /** * Creates a new dictionary with the contents of the fields named field * for the terms, payloadField for the corresponding payloads * and uses the weightsValueSource supplied to determine the * score. */ public DocumentValueSourceDictionary(IndexReader reader, String field, ValueSource weightsValueSource, String payload) { super(reader, field, null, payload); this.weightsValueSource = weightsValueSource; } /** * Creates a new dictionary with the contents of the fields named field * for the terms and uses the weightsValueSource supplied to determine the * score. */ public DocumentValueSourceDictionary(IndexReader reader, String field, ValueSource weightsValueSource) { super(reader, field, null, null); this.weightsValueSource = weightsValueSource; } @Override public InputIterator getEntryIterator() throws IOException { return new DocumentValueSourceInputIterator(payloadField!=null); } final class DocumentValueSourceInputIterator extends DocumentDictionary.DocumentInputIterator { private FunctionValues currentWeightValues; /** leaves of the reader */ private final List leaves; /** starting docIds of all the leaves */ private final int[] starts; /** current leave index */ private int currentLeafIndex = 0; public DocumentValueSourceInputIterator(boolean hasPayloads) throws IOException { super(hasPayloads); leaves = reader.leaves(); starts = new int[leaves.size() + 1]; for (int i = 0; i < leaves.size(); i++) { starts[i] = leaves.get(i).docBase; } starts[leaves.size()] = reader.maxDoc(); currentWeightValues = (leaves.size() > 0) ? weightsValueSource.getValues(new HashMap(), leaves.get(currentLeafIndex)) : null; } /** * Returns the weight for the current docId as computed * by the weightsValueSource * */ @Override protected long getWeight(Document doc, int docId) { if (currentWeightValues == null) { return 0; } int subIndex = ReaderUtil.subIndex(docId, starts); if (subIndex != currentLeafIndex) { currentLeafIndex = subIndex; try { currentWeightValues = weightsValueSource.getValues(new HashMap(), leaves.get(currentLeafIndex)); } catch (IOException e) { throw new RuntimeException(); } } return currentWeightValues.longVal(docId - starts[subIndex]); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy