How to access doc values from expert script plugin

I am trying to write an expert script plugin following the instructions given in

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-engine.html

"mappings": {
      "type": {
         "properties": {
            "blob": {
                "type": "binary"
            }            
         }
      }
}

I created 3 documents with sample documents and made the blob field a byte[] in my java code. And wrote the documents to my index. I have the following code in my plugin and i always get "null" where i print "Loaded docValues. docValues = ". Can anyone advice me whats wrong ?

                    @Override
                    public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
                        PostingsEnum postings = context.reader().postings(new Term(field, term));
                        BinaryDocValues docValues = context.reader().getBinaryDocValues(blobField);
                        logger.info("Loaded docValues. docValues = "+docValues);

                        if (postings == null) {
                            // the field and/or term don't exist in this segment, so always return 0
                            logger.info("the field and/or term don't exist in this segment, so always return 0");
                            return () -> 0.0d;
                        }
                        return new LeafSearchScript() {
                            int currentDocid = -1;

                            @Override
                            public void setDocument(int docid) {
                                // advance has undefined behavior calling with a docid <= its current docid
                                if (postings.docID() < docid) {
                                    try {
                                        postings.advance(docid);
                                    } catch (IOException e) {
                                        throw new UncheckedIOException(e);
                                    }
                                }
                                currentDocid = docid;
                            }

                            @Override
                            public double runAsDouble() {
                                BytesRef bytesRef = null;
                                double score = 1.0;
                                if (docValues == null) {
                                    logger.info("docValues is null");
                                    return score;
                                }
                                try {
                                    logger.info("getting docValues for docId="+currentDocid);
                                    bytesRef = docValues.get(currentDocid);
                                    return score;
                                } catch (IOException e) {
                                    throw new UncheckedIOException(e);
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                                return score;
                            }
                        };
                    }

                    @Override
                    public boolean needsScores() {
                        return false;
                    }
                };
            }
            throw new IllegalArgumentException("Unknown script name " + scriptSource);
        }

Thanks
Srini

Binary fields do not have doc values by default. You need to enable them on your field. LeafReader.getBinaryDocValues returns null when binary doc values do not exist for the given field.

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