QueryMinSlideWindow

更新时间:
复制 MD 格式

QueryMinSlideWindow measures how tightly a search query's terms cluster within a field. A higher ratio means the query terms appear close together — a strong indicator of relevance.

Technically, it calculates the ratio of the number of terms that a search query hits in a specific field to the minimum window (the shortest span of term positions) that covers all matched terms. The return value falls in the range [0, 1].

Example: After analysis, the title field contains four terms with IDs 0 (Open), 1 (Search), 2 (User), and 3 (Manual). A query matches terms 1 (Search) and 3 (Manual) — two hits. The minimum window spanning positions 1 through 3 is 3. The ratio is 2/3 ≈ 0.667.

Methods

SignatureDescription
QueryMinSlideWindow create(OpsScorerInitParams params, CString indexName, CString fieldName)Creates a QueryMinSlideWindow object.
QueryMinSlideWindow create(OpsScorerInitParams params, CString indexName, CString fieldName, boolean inOrder)Creates a QueryMinSlideWindow object with explicit control over whether term order is enforced during window calculation.
double evaluate(OpsScoreParams params)Returns the ratio of query-matched terms to the minimum window size in the field. Valid range: [0, 1].

Method details

create (3-parameter variant)

Creates a QueryMinSlideWindow object.

Parameters

ParameterTypeConstraintsDescription
paramsOpsScorerInitParamsThe parameters used for score calculation. See OpsScorerInitParams.
indexNameCStringMust be a constant. Supported analyzers: general analyzer for Chinese, custom analyzer, single character analyzer for Chinese, analyzer for English, analyzer for fuzzy searches.The name of the index.
fieldNameCStringMust be a constant. Field must be of type TEXT or SHORT_TEXT.The name of the field in the specified index.

create (4-parameter variant)

Creates a QueryMinSlideWindow object with explicit control over term order during window calculation.

Parameters

ParameterTypeConstraintsDescription
paramsOpsScorerInitParamsThe parameters used for score calculation. See OpsScorerInitParams.
indexNameCStringMust be a constant. Supported analyzers: general analyzer for Chinese, custom analyzer, single character analyzer for Chinese, analyzer for English, analyzer for fuzzy searches.The name of the index.
fieldNameCStringMust be a constant. Field must be of type TEXT or SHORT_TEXT.The name of the field in the specified index.
inOrderbooleanValid values: true, false.Specifies whether to enforce term order when calculating the minimum window. Set to true for order-sensitive languages or phrase-like queries. Set to false for bag-of-words matching where term position does not matter.

evaluate

Returns the ratio of the number of terms that the search query hits in the field to the minimum window of the search query in the field.

Parameter

ParameterTypeDescription
paramsOpsScoreParamsThe parameters used for score calculation. See OpsScoreParams.

Return value: double in the range [0, 1]. A value closer to 1 means the matched terms cluster tightly in the field.

Example

The following example creates two QueryMinSlideWindow instances for the same field — one without order enforcement (_f1) and one with order enforcement (_f2) — and combines their scores.

Use _f1 (order-insensitive) when the relevance of a document does not depend on the sequence of matched terms, such as keyword searches. Use _f2 (order-sensitive) when term proximity in a specific order matters, such as phrase-like queries in languages where word order is significant.

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.similarity.distribution.QueryMinSlideWindow;

class BasicSimilarityScorer {
    QueryMinSlideWindow _f1;
    QueryMinSlideWindow _f2;
    boolean init(OpsScorerInitParams params) {
        _f1 = QueryMinSlideWindow.create(params, "pack_index1", "text_field");
        _f2 = QueryMinSlideWindow.create(params, "pack_index1", "text_field", true);
        return true;
    }

    double score(OpsScoreParams params) {
        return _f1.evaluate(params) + _f2.evaluate(params);
    }
}