Elasticsearch多个查询值

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

Elasticsearch multiple query values

问题

你好,我正在尝试使用Elasticsearch创建一个查询:

<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.5.3</version>
</dependency>

我需要查询以多个值为基础选择多个文件:

query = MatchQuery.of(m -> m
                    .field("fileType")
                    .query("EXP", "WAIT_EXP")
                )._toQuery();

但这给我一个语法错误。查询只获取1个值。我需要获取fileType为exp或wait_exp的文件。

我阅读了这里的文档 https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html,但找不到有用的信息。

英文:

Hi I am trying to create a query with elastic search :

&lt;dependency&gt;
            &lt;groupId&gt;co.elastic.clients&lt;/groupId&gt;
            &lt;artifactId&gt;elasticsearch-java&lt;/artifactId&gt;
            &lt;version&gt;8.5.3&lt;/version&gt;
&lt;/dependency&gt;

I need the query to select multiple files based on values:

query = MatchQuery.of(m -&gt; m
                        .field(&quot;fileType&quot;)
                        .query(&quot;EXP&quot;, &quot;WAIT_EXP&quot;)
                    )._toQuery();

But this is giving me an syntax error. The query only gets 1 value. I need the get files with fileType both exp or wait_exp.

I read the documentation at here https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html but cant find anything helpfull.

答案1

得分: 1

你可以使用布尔查询(bool query)。此外,你可以查看这个线程

SearchRequest request = new SearchRequest("your_index");

BoolQuery boolQuery = new BoolQuery();

boolQuery.should(MatchQuery.of(m -> m.field("fileType").query("EXP")));
boolQuery.should(MatchQuery.of(m -> m.field("fileType").query("WAIT_EXP")));

request.query(boolQuery);
英文:

You can use bool query. Also you can check this thread.

SearchRequest request = new SearchRequest(&quot;your_index&quot;);

BoolQuery boolQuery = new BoolQuery();

boolQuery.should(MatchQuery.of(m -&gt; m.field(&quot;fileType&quot;).query(&quot;EXP&quot;)));
boolQuery.should(MatchQuery.of(m -&gt; m.field(&quot;fileType&quot;).query(&quot;WAIT_EXP&quot;)));

request.query(boolQuery);

huangapple
  • 本文由 发表于 2023年7月31日 19:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803116.html
匿名

发表评论

匿名网友

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

确定