0

My documents have a keyword which value is "", how can I search it by java Elasticsearch?

No effect like the code is :

query.must(QueryBuilders.boolQuery()
            .should(QueryBuilders.regexpQuery("paymentRetCode", ""))
            );

1 Answer 1

1

You need to use a script query in order to check that the length of the string is 0:

Script script = new Script(ScriptType.INLINE, "myscript", "doc.paymentRetCode.value.length() == 0", new HashMap())
query.must(QueryBuilders.boolQuery()
    .should(QueryBuilders.scriptQuery(script))))
);

Script queries are not ideal, though, you should set null instead of the empty string if you need to detect documents with empty strings, in which case you'll be able to use a negated exists query:

QueryBuilders.boolQuery()
    .mustNot(QueryBuilders.existsQuery("paymentRetCode"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.