Description
Today when using @ValueConverter
to convert a field customly, I found that spring-data-elasticsearch will invoke PropertyValueConverter#write
twice if query return a Stream
or Collection
in this method.
For example, using this entity(I renamed it as Sample
, and defined prefix as foo_
) and custom FooConverter, and create a repository class:
public interface SampleRepository extends ElasticsearchRepository<Sample, String> {
Stream<Sample> findBySomeField(String someField);
// this method uses findAllBy to just avoid duplicate with findBy in the last method
List<Sample> findAllBySomeField(String someField);
Sample findSampleBySomeField(String someField);
}
If the query parameter is what
,
the first method will generate the query as:
TRACE [main] tracer [RequestLogger.java:90] curl -iX POST 'http://localhost:9200/foo/_search?scroll=60000ms&typed_keys=true&max_concurrent_shard_requests=5&search_type=query_then_fetch&batched_reduce_size=512' -d '{"from":0,"size":500,"query":{"bool":{"must":[{"query_string":{"query":"foo_foo_what","fields":["someField^1.0"],"type":"best_fields","default_operator":"and","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"explain":false}'
TRACE [main] tracer [RequestLogger.java:90] curl -iX DELETE 'http://localhost:9200/_search/scroll' -d '{"scroll_id":["FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFnd1aXZSRV9NU1pTSHZVUUs4RWNzbXcAAAAAAAAC1BZBd2NoaTNUV1RPbWx3RW9kVGxRVW1B"]}'
the prefix foo_
will be added twice so the query string becomes into foo_foo_what
.
The second and the third do the same:
TRACE [main] tracer [RequestLogger.java:90] curl -iX POST 'http://localhost:9200/foo/_search?typed_keys=true&max_concurrent_shard_requests=5&search_type=query_then_fetch&batched_reduce_size=512' -d '{"from":0,"size":0,"query":{"bool":{"must":[{"query_string":{"query":"foo_foo_what","fields":["someField^1.0"],"type":"best_fields","default_operator":"and","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"explain":false,"track_total_hits":2147483647}'
also as foo_foo_what
.
If prefix is foo-
as the post wrote originally, the query is stranger:
TRACE [main] tracer [RequestLogger.java:90] curl -iX POST 'http://localhost:9200/foo/_search?scroll=60000ms&typed_keys=true&max_concurrent_shard_requests=5&search_type=query_then_fetch&batched_reduce_size=512' -d '{"from":0,"size":500,"query":{"bool":{"must":[{"query_string":{"query":"foo\\-foo\\-what","fields":["someField^1.0"],"type":"best_fields","default_operator":"and","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"explain":false}'
the query string becomes foo\\-foo\\-what
, not only double foo-
, but also add slashes as foo\\-
.
I think converting query criterias once should be as expected, rather than twice.