2

I am using spring-boot-starter-data-elasticsearch 2.1.0.RC1.

I have a custom query (via @Query annotation) where I want to pass 11 parameters. The query looks like this:

{
      "bool" : {
        "must" : [
          {
            "range" : {
              "attribute0" : { "gte" : ?0, "lte" : ?1 }
            }
          },
          {
            "range" : {
              "attribute1" : { "gte" : ?2, "lte" : ?3 }
            }
          },
          {
            "term": { "attribute2": "?4" }
          },
          {
            "term": { "attribute3": "?5" }
          },
          {
            "term": { "attribute4": "?6" }
          },
          {
            "term": { "attribute5": "?7" }
          },
          {
            "term": { "attribute6": "?8" }
          },
          {
            "term": { "attribute7": "?9" }
          },
          {
            "term": { "attribute8": "?10" }
          }
        ]
      }
    }

In my repository, it looks like this:

@Query("{\"bool\":{\"must\":[{\"range\":{\"attribute0\":{\"gte\":?0,\"lte\":?1}}},{\"range\":{\"attribute1\":{\"gte\":?2,\"lte\":?3}}},{\"term\":{\"attribute2\":\"?4\"}},{\"term\":{\"attribute3\":\"?5\"}},{\"term\":{\"attribute4\":\"?6\"}},{\"term\":{\"attribute5\":\"?7\"}},{\"term\":{\"attribute6\":\"?8\"}},{\"term\":{\"attribute7\":\"?9\"}},{\"term\":{\"attribute8\":\"?10\"}}]}}")
Page<Entity> findAllByAttributes(
          Integer param0, Integer param1, 
          Integer param2, Integer param3, 
          String param4, String param5,
          String param6, String param7,
          String param8, String param9,
          String param10, Pageable pageable);

The problem is, I can't use more than 10 parameters (single digit index ?0 to ?9). The placeholder ?10 gets resolved to placeholder 1 with a zero appended.

I can't find anything in the Spring Data Elasticsearch reference documentation on any limitations in the number of parameters.

How can I pass more than 10 parameters into the @Query?

2
  • Hmm weird, since the pattern used to figure out the parameters is "\?(\d+)" (as can be seen here) Commented Oct 29, 2018 at 7:10
  • I added the repository method definition. Maybe it has something to do with the escaping/preprocessing of the query? Commented Oct 29, 2018 at 8:47

2 Answers 2

5

Spring Data is the very big project in way of playing with data world

As I know, We can have one more way to do the same thing in different way.

Spring data has one more annotation which would help to treat the method parameter as query parameter.

Annotation: @Param

This annotation can be used with the method parameter itself. Please find below format which can help to extend the method parameter count to use with spring data.

Explaination: You can include N no. of method parameter with @Param annotation. Please find below valid expression to write annotation.

@Param("param0")

Now, when you add this @Param annotation with method parameter, you must need to specify parameter in @Query with the prefix of colon (:). Something like param0 in below query,

@Query("{"query": {"bool": {"must": [{ "match": {"userId": ":param0" }}]}}}")

Complete Example: You can see org_id is the query parameter.

@Query("{"query": {"bool": {"must": [{ "match": {"userId": ":org_Id"}}]}}}")
List<User> getByOrgId(@Param("org_Id") String orgId)
Sign up to request clarification or add additional context in comments.

Comments

1

I have this issue as well. I'll file a bug.

poor mans fix try using numbering like ?00, ?01, ?02

Digging in the code the issue is not in the regular expression but the replace command seen here

result.replace(group, getParameterWithIndex(accessor, index));

which of course will replace all and thinks ?1 needs to be replaced in both ?1 and ?10

1 Comment

please check my comments on your PR

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.