Elasticsearch plugin開(kāi)發(fā) 之 自定義payload_score query

當(dāng)需要將term的權(quán)重存儲(chǔ)到索引中時(shí)乘综,需要保存成payload的格式:

源代碼:https://github.com/limingnihao/elasticsearch-reference/tree/master/Examples
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.10/analysis-delimited-payload-tokenfilter.html

類似于:

the|0 brown|3 fox|4 is|0 quick|10

查詢的時(shí)候,如果需要用到保存好的value卡辰,則需要lucene 的PayloadScoreQuery或者PayloadCheckQuery。

PayloadScoreQuery:

首先查看下lucene的PayloadScoreQuery的構(gòu)造方法:


  /**
   * Creates a new PayloadScoreQuery
   * @param wrappedQuery the query to wrap
   * @param function a PayloadFunction to use to modify the scores
   * @param decoder a PayloadDecoder to convert payloads into float values
   * @param includeSpanScore include both span score and payload score in the scoring algorithm
   */
  public PayloadScoreQuery(SpanQuery wrappedQuery, PayloadFunction function, PayloadDecoder decoder, boolean includeSpanScore) {
    this.wrappedQuery = Objects.requireNonNull(wrappedQuery);
    this.function = Objects.requireNonNull(function);
    this.decoder = Objects.requireNonNull(decoder);
    this.includeSpanScore = includeSpanScore;
  }

可以發(fā)現(xiàn)九妈,需要構(gòu)造4個(gè)參數(shù):

  • SpanQuery wrappedQuery。進(jìn)行召回的query萌朱,必須是spanQuery
  • PayloadFunction function。當(dāng)命中多個(gè)term時(shí)嚷兔,得分的計(jì)算規(guī)則,max冒晰、min同衣、sum壶运、
  • PayloadDecoder decoder。保存的value的解碼方式蒋情。int或float類型
  • boolean includeSpanScore。是否使用保存的分?jǐn)?shù)棵癣。

下面開(kāi)始開(kāi)發(fā),需要構(gòu)建2個(gè)類一個(gè)是plugin狈谊、一個(gè)是builder

PayloadScoreQParserPlugin

用于構(gòu)造Builder的

public class PayloadScoreQParserPlugin extends Plugin implements SearchPlugin {

    @Override
    public List<QuerySpec<?>> getQueries() {
        return Collections.singletonList(
            new QuerySpec<>(PayloadScoreQueryBuilder.NAME, PayloadScoreQueryBuilder::new, PayloadScoreQueryBuilder::fromXContent)
        );
    }
}

PayloadScoreQueryBuilder

首先解析參數(shù)的fromXContent方法:

主要用于解析我們自定義的參數(shù):query、func河劝、calc(后續(xù)擴(kuò)展權(quán)重交叉計(jì)算)壁榕、includeSpanScore

public static QueryBuilder fromXContent(XContentParser parser) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = null;

    String func = null;
    String calc = null;
    boolean includeSpanScore = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                iqb = parseInnerQueryBuilder(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                    "[" + NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (FUNC_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                func = parser.text();
            } else if (CALC_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                calc = parser.text();
            } else if (INCLUDE_SPAN_SCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                includeSpanScore = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                    "[" + NAME + "] query does not support [" + currentFieldName + "]");
            }
        }
    }
    return new PayloadScoreQueryBuilder(iqb, func, calc, includeSpanScore);
}

構(gòu)造PayloadScoreQuery的doToQuery方法:

主要是將lucene的PayloadScoreQuery類需要的4個(gè)參數(shù)構(gòu)造出來(lái):

protected Query doToQuery(SearchExecutionContext context) throws IOException {
    // query  parse
    SpanQuery spanQuery = null;
    try {
        spanQuery = (SpanQuery) query.toQuery(context);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

    if (spanQuery == null) {
        throw new IllegalArgumentException("SpanQuery is null");
    }

    PayloadFunction payloadFunction = PayloadUtils.getPayloadFunction(this.func);
    if (payloadFunction == null) {
        throw new IllegalArgumentException("Unknown payload function: " + func);
    }
    PayloadDecoder payloadDecoder = PayloadUtils.getPayloadDecoder("float");

    return new PayloadScoreQuery(spanQuery, payloadFunction, payloadDecoder, this.includeSpanScore);
}

PayloadScoreQueryBuilder完整代碼

package org.elasticsearch.plugins.payload;

import org.apache.lucene.queries.payloads.PayloadDecoder;
import org.apache.lucene.queries.payloads.PayloadFunction;
import org.apache.lucene.queries.payloads.PayloadScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.*;

import java.io.IOException;
import java.util.Objects;

public class PayloadScoreQueryBuilder extends AbstractQueryBuilder<PayloadScoreQueryBuilder> {
    public static final String NAME = "payload_score";

    private static final ParseField QUERY_FIELD = new ParseField("query");
    private static final ParseField FUNC_FIELD = new ParseField("func");
    private static final ParseField CALC_FIELD = new ParseField("calc");
    private static final ParseField INCLUDE_SPAN_SCORE_FIELD = new ParseField("includeSpanScore");

    private final QueryBuilder query;
    private final String func;
    private final String calc;
    private final boolean includeSpanScore;

    public PayloadScoreQueryBuilder(QueryBuilder query, String func, String calc, boolean includeSpanScore) {
        this.query = requireValue(query, "[" + NAME + "] requires '" + QUERY_FIELD.getPreferredName() + "' field");
        this.func = func;
        this.calc = calc;
        this.includeSpanScore = includeSpanScore;
    }

    public PayloadScoreQueryBuilder(StreamInput in) throws IOException {
        super(in);
        this.query = in.readNamedWriteable(QueryBuilder.class);
        this.func = in.readString();
        this.calc = in.readString();
        this.includeSpanScore = in.readBoolean();
    }

    @Override
    protected void doWriteTo(StreamOutput out) throws IOException {
        out.writeNamedWriteable(query);
        out.writeString(this.func);
        out.writeString(this.calc);
        out.writeBoolean(this.includeSpanScore);
    }

    @Override
    protected void doXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject(NAME);
        builder.field(QUERY_FIELD.getPreferredName());
        query.toXContent(builder, params);

        builder.field(FUNC_FIELD.getPreferredName(), this.func);
        builder.field(CALC_FIELD.getPreferredName(), this.calc);
        builder.field(INCLUDE_SPAN_SCORE_FIELD.getPreferredName(), this.includeSpanScore);
        printBoostAndQueryName(builder);
        builder.endObject();
    }

    public static QueryBuilder fromXContent(XContentParser parser) throws IOException {
        String currentFieldName = null;
        XContentParser.Token token;
        QueryBuilder iqb = null;

        String func = null;
        String calc = null;
        boolean includeSpanScore = false;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    iqb = parseInnerQueryBuilder(parser);
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                        "[" + NAME + "] query does not support [" + currentFieldName + "]");
                }
            } else if (token.isValue()) {
                if (FUNC_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    func = parser.text();
                } else if (CALC_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    calc = parser.text();
                } else if (INCLUDE_SPAN_SCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    includeSpanScore = parser.booleanValue();
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                        "[" + NAME + "] query does not support [" + currentFieldName + "]");
                }
            }
        }
        return new PayloadScoreQueryBuilder(iqb, func, calc, includeSpanScore);
    }

    @Override
protected Query doToQuery(SearchExecutionContext context) throws IOException {
    // query  parse
    SpanQuery spanQuery = null;
    try {
        spanQuery = (SpanQuery) query.toQuery(context);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

    if (spanQuery == null) {
        throw new IllegalArgumentException("SpanQuery is null");
    }

    PayloadFunction payloadFunction = PayloadUtils.getPayloadFunction(this.func);
    if (payloadFunction == null) {
        throw new IllegalArgumentException("Unknown payload function: " + func);
    }
    PayloadDecoder payloadDecoder = PayloadUtils.getPayloadDecoder("float");

    return new PayloadScoreQuery(spanQuery, payloadFunction, payloadDecoder, this.includeSpanScore);
}

    @Override
    protected boolean doEquals(PayloadScoreQueryBuilder that) {
        return Objects.equals(query, that.query)
            && Objects.equals(func, that.func)
            && Objects.equals(calc, that.calc)
            && Objects.equals(includeSpanScore, that.includeSpanScore);
    }

    @Override
    protected int doHashCode() {
        return Objects.hash(query, func, calc, includeSpanScore);
    }

    @Override
    public String getWriteableName() {
        return NAME;
    }

}

執(zhí)行示例:

POST http://127.0.0.1:9200/position/_search
{
    "query": {
        "payload_score": {
            "func": "sum",
            "calc": "sum",
            "includeSpanScore": "false",
            "query": {
                "span_or": {
                    "clauses": [
                        {
                            "span_term": {
                                "FIELD": "test"
                            }
                        }
                    ]
                }
            }
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末务甥,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子敞临,更是在濱河造成了極大的恐慌,老刑警劉巖哟绊,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異票髓,居然都是意外死亡攀涵,警方通過(guò)查閱死者的電腦和手機(jī)洽沟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)裆操,“玉大人炉媒,你說(shuō)我怎么就攤上這事±ニ福” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵静尼,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我鼠渺,道長(zhǎng)鸭巴,這世上最難降的妖魔是什么拦盹? 我笑而不...
    開(kāi)封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮普舆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘奔害。我一直安慰自己楷兽,他們只是感情好华临,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著雅潭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扶供。 梳的紋絲不亂的頭發(fā)上筛圆,一...
    開(kāi)封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天太援,我揣著相機(jī)與錄音,去河邊找鬼扳碍。 笑死,一個(gè)胖子當(dāng)著我的面吹牛笋敞,可吹牛的內(nèi)容都是我干的碱蒙。 我是一名探鬼主播,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼哀墓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了喷兼?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤褒搔,失蹤者是張志新(化名)和其女友劉穎喷面,沒(méi)想到半個(gè)月后星瘾,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體惧辈,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年盒齿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片边翁。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡翎承,死狀恐怖符匾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情啊胶,我是刑警寧澤甸各,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布趣倾,位于F島的核電站,受9級(jí)特大地震影響某饰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜黔漂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一碧浊、第九天 我趴在偏房一處隱蔽的房頂上張望瘟仿。 院中可真熱鬧,春花似錦劳较、人聲如沸驹止。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至抖仅,卻和暖如春坊夫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背环凿。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留智听,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓渡紫,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親惕澎。 傳聞我的和親對(duì)象是個(gè)殘疾皇子莉测,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容