what is the equivalent PercolateQueryBuilder in spring data elastic search?

436 Views Asked by At

Bottom line - How do I turn my simple percolator query to spring data elastic search? Any leads are appreciated. Thanks

I have been trying to write the below percolate query using spring data elastic search

"query": {
    "percolate": {
      "field": "query",
      "document": {
        "message": "A new bonsai tree in the office"
      }
    }
  }

I tried

Criteria criteria = new Criteria("query").is(

            new Criteria("percolate")
                                .is(
                                        new Criteria("field").is("query")
                                                .and("document")
                                                .is(new Criteria("message").is(text))
                                )
    );

Tried

Query query = new StringQuery(String.format("{\n" +
                "  \"query\": {\n" +
                "    \"percolate\": {\n" +
                "      \"field\": \"query\",\n" +
                "      \"document\": {\n" +
                "        \"message\": \"%s\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}",text));

Also tried NativeSearchQuery but none of them worked.

I see https://tothepoint.group/matching-documents-with-elasticsearch-percolator-query/ where the author uses PercolateQueryBuilder (https://github.com/ophalsp/elasticsearch-percolator ) but I dont see PercolateQueryBuilder in spring data elastic search.

EDIT:- I tried all the builders that implement QueryBuilder. Since none of them have query or percolate in the XContentBuilder builder (as shown below) they do not work

protected void doXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject("query");

I can also see that PercolateQueryBuilder is part of percolator-client that is part of org.elasticsearch.client.transport which is excluded in spring-boot

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-elasticsearch</artifactId>
      <version>4.2.1</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>transport</artifactId>
          <groupId>org.elasticsearch.client</groupId>
        </exclusion>
      </exclusions>
    </dependency>

I will add org.elasticsearch.client.transport and give it a shot.

1

There are 1 best solutions below

1
On

Answering my own question with the success I had. Might help someone.

After adding the below dependency

<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>7.12.1</version>
        </dependency>

and with the below code I was able to run query on the Percolator. I dont know why spring-boot removed the above dependency. So far I dint face any issue. If I face issue because of the above dependency I will update here.

public Query buildLookUpQuery(String text){
        XContentBuilder docBuilder = null;
        try {
            docBuilder = XContentFactory.jsonBuilder().startObject();
            docBuilder.field("message", text);
            docBuilder.endObject();
        } catch (IOException ioException) {
            log.error("Unable to build a query ", ioException.getMessage());
        }
        PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder(QUERY,
                BytesReference.bytes(docBuilder),
                XContentType.JSON);
        return new NativeSearchQueryBuilder()
                .withQuery(percolateQuery)
                .build();
    }