Writing queries in Elasticsearch's Java client

I used Elastic Search java client,
I am implementing a search application.
(Index has already been built)

I want to reproduce the Elastic Search java client, the following query.

However, it can not be reproduced.

If you are familiar with java client,
I want you to tell me.

The query I want to execute (sample field has java and php)

{
    "Query": {
        "Bool": {
            "Must": [{
                "Match": {
                    "Sample": "java"
                }},
                {"Match": {
                    "Sample": "php"
                }
            }]
        }
    },
    "Size": 50
}

Assembling queries on the java client

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery ().
        Must (QueryBuilders.matchQuery ("sample", "test")));
SearchRequestBuilder builder = client.prepareSearch ()
        .setSize (50)
        .setQuery (boolQuery);
System.out.println (builder);

↑ Output contents of "builder" variable

{
  "Size": 50,
  "Query": {
    "Bool": {
      "Must": [
        {
          "Match": {
            "Sample": {
              "Query": "test",
              "Operator": "OR",
              "Prefix_length": 0,
              "Max_expansions": 50,
              "Fuzzy_transpositions": true,
              "Lenient": false,
              "Zero_terms_query": "NONE",
              "Boost": 1.0
            }
          }
        }
      ],
      "Disable_coord": false,
      "Adjust_pure_negative": true,
      "Boost": 1.0
    }
  },
  "Ext": {}
}

When outputting, extra properties are included in match.
Moreover, I still do not know how to match multiple words.

Someone, please lend me your wisdom.

Environment

  • Language: Java 8
  • Framework: Spring
  • Elasticsearch client (jar) ver
    • Org.elasticsearch: 5.1.1
    • Org.elasticsearch.client: 5.1.1
  • Elasticsearch's ver: 5.3.1

Just call must() another time.

See https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html#java-query-dsl-bool-query

1 Like

I was saved very much.
thank you!

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