使用Java,如何将Elasticsearch中match查询的默认运算符更改为AND?

huangapple go评论83阅读模式
英文:

Using java, how to change the default operator of match query to AND in elasticsearch?

问题

当使用以下的Java API时

    query.must(matchQuery("name", object.getName()));

生成的Elasticsearch查询为

    "bool":{
        "must":[
          {"match":{"name":{"query":"De Michael Schuster","operator":"OR","boost":1.0}}}
    .....
目前我得到的文档名称为De OR Michael OR Schuster正如预期的那样
我想将操作符更改为AND以匹配整个字符串

我知道我可以使用term查询但在我的情况下这不是一个选项

我找到了这个帖子但是没有给出答案 - https://discuss.elastic.co/t/changing-the-default-operator-for-search-api/47033

请问如何在Java中实现这一点
英文:

When using java api as below

query.must(matchQuery("name", object.getName()));

The resulted elastic query is

"bool":{
    "must":[
      {"match":{"name":{"query":"De Michael Schuster","operator":"OR","boost":1.0}}}
.....

Right now I am getting back document with name : De OR Michael OR Schuster as expected.
I want to change the operator to AND to match the whole string.

I know I can use term query, but that is not an option in my scenario.

I came across this, but the answer is not given - https://discuss.elastic.co/t/changing-the-default-operator-for-search-api/47033

How can I achieve this using Java ?

答案1

得分: 0

仅仅像这样:

query.must(matchQuery("name", object.getName()).operator(Operator.AND));
英文:

Simply like this:

query.must(matchQuery("name", object.getName()).operator(Operator.AND));

huangapple
  • 本文由 发表于 2020年9月14日 20:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63884843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定