How to get list as term value in params for custom script plugin

I'm making custom score script plugin for scoring based on payload value.

I've done with scoring logic, but it's working properly when it receives only one term.

I want to receive several analyzed terms in query. How can I receive several terms as array or list in params?

params: {"field": "field", "term": ["term1", "term2", "term3"]}

i want this type of params to work.

here is my code. I followed script example in elasticsearch github.
i'm working with gradle and java 14.

private PayloadScoreLeafFactory(Map<String, Object> params, SearchLookup lookup) {
            if (!params.containsKey("field")) {
                throw new IllegalArgumentException("Missing parameter [field]");
            }
            if (!params.containsKey("term")) {
                throw new IllegalArgumentException("Missing parameter [term]");
            }
            this.params = params;
            this.lookup = lookup;
            field = params.get("field").toString();
            term = params.get("term").toString();
        }

        @Override
        public boolean needs_score() {
            return false;
        }

        @Override
        public ScoreScript newInstance(LeafReaderContext context) throws IOException {
            PostingsEnum postings = context.reader().postings(new Term(field, term), PostingsEnum.PAYLOADS);
            if (postings == null) {
                return new ScoreScript(params, lookup, context) {
                    @Override
                    public double execute(ExplanationHolder explanation) {
                        return 0.0d;
                    }
                };
            }
            return new ScoreScript(params, lookup, context) {
                int currentDocid = -1;
                @Override
                public void setDocument(int docid) {
                    if (postings.docID() < docid) {
                        try {
                            postings.advance(docid);
                        } catch (IOException e) {
                            throw new UncheckedIOException(e);
                        }
                    }
                    currentDocid = docid;
                }

                @Override
                public double execute(ExplanationHolder explanation) {
                    if (postings.docID() != currentDocid) {
                        return 0.0d;
                    }
                    try {
                        int freq = postings.freq();
                        float sum_of_payload = 0.0f;
                        for (int i=0; i<freq; i++) {
                            postings.nextPosition();
                            BytesRef payload = postings.getPayload();
                            if (payload != null) {
                                sum_of_payload += ByteBuffer.wrap(payload.bytes, payload.offset, payload.length)
                                        .order(ByteOrder.BIG_ENDIAN).getFloat();
                            }
                        }
                        return sum_of_payload;
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }
            };
        }
    }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.