Skip to content

How to Implement Complex Queries (Vector Queries) in Spring Data Elasticsearch #3039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MrWangGang opened this issue Jan 8, 2025 · 3 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@MrWangGang
Copy link

The following code will throw an exception: [es/search] failed: [parsing_exception] unknown query [query].


    @Query(value = """
{
  "query": {
    "script_score": {
      "query": {
        "match_all": {}
      },
      "script": {
        "source": "cosineSimilarity(params.vector, doc['nameVector']) + cosineSimilarity(params.vector, doc['descriptionVector'])",
        "params": {
          "vector": ?0
        }
      }
    }
  },
  "sort": [
    { "_score": { "order": "desc" } }
  ]
}
""")
Flux<SearchHit<ClubPO>> queryClubs(List<Double> vector);

The following code can run successfully.


    @Query("""
{
  "script_score": {
    "query": {
      "match_all": {}
    },
    "script": {
      "source": "cosineSimilarity(params.vector, doc['nameVector']) + cosineSimilarity(params.vector, doc['descriptionVector'])",
      "params": {
        "vector": ?0
      }
    }
  }
}
""")
    Flux<SearchHit<ClubPO>> queryClubs(List<Double> vector);

What should I do?

Are there any other annotations? For example, ones that support more raw query syntax?

{"error":{"root_cause":[{"type":"parsing_exception","reason":"unknown query [query]","line":2,"col":12}],"type":"parsing_exception","reason":"unknown query [query]","line":2,"col":12,"caused_by":{"type":"named_object_not_found_exception","reason":"[2:12] unknown field [query]"}},"status":400}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 8, 2025
@MrWangGang
Copy link
Author

Even though the query works, why is the returned _score NaN?

@MrWangGang
Copy link
Author

@praseodym

@sothawo
Copy link
Collaborator

sothawo commented Jan 10, 2025

As stated in the documentation:

The String that is set as the annotation argument must be a valid Elasticsearch JSON query. It will be sent to Easticsearch as value of the query element

You can add a parameter of type org.springframework.data.domain.Sort to your repository function to define the sort.

@sothawo sothawo closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2025
sothawo added a commit that referenced this issue Apr 26, 2025
Original Pull Request #3099
Closes: #3039

Signed-off-by: Peter-Josef Meisch <[email protected]>
sothawo added a commit that referenced this issue Apr 26, 2025
Original Pull Request #3099
Closes: #3039

Signed-off-by: Peter-Josef Meisch <[email protected]>
(cherry picked from commit a07ac3c)
sothawo added a commit that referenced this issue Apr 26, 2025
Original Pull Request #3099
Closes: #3039

Signed-off-by: Peter-Josef Meisch <[email protected]>
(cherry picked from commit a07ac3c)
(cherry picked from commit 0e8401d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants