简体   繁体   English

弹簧数据弹性搜索带有特殊字符

[英]Spring Data Elastic Search with special characters

As part of our project we are using Spring Data on top of Elastic Search. 作为我们项目的一部分,我们在弹性搜索之上使用Spring Data。 We found very interesting issue with findBy queries. 我们发现findBy查询非常有趣。 If we pass string that contains space it didn't find the right element unless we pad the string with quotes. 如果我们传递包含空格的字符串,除非我们用引号填充字符串,否则它找不到正确的元素。 For example: for getByName(String name) we should pass getByName("\\"John Do\\""). 例如:对于getByName(String name),我们应该传递getByName(“\\”John Do \\“”)。 Is there any way to eliminate such redundant padding? 有没有办法消除这种冗余填充?

I'm trying my first steps with Spring (Boot Starter) Data ES and stumbled upon the same issue as you have, only in my case it was a : that 'messed things up'. 我正在尝试使用Spring(Boot Starter)Data ES的第一步,偶然发现了与你相同的问题,只是在我的情况下它是:“搞砸了”。 I've learned that this is part of the reserved characters ( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters ). 我了解到这是保留字符的一部分( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters )。 The quoting that you mention is exactly the solution I use for now. 你提到的引用正是我现在使用的解决方案。 It results in a query like this: 它会产生如下查询:

{ "from": 0, "query": { "bool": { "must": { "query_string": { "query": "\\"John Do\\"", "fields": ["name"] } } } } }

(You can use this in a rest console or in ElasticHQ to check the result.) A colleague suggested that switching to a 'term' query: (您可以在休息控制台或ElasticHQ中使用它来检查结果。)一位同事建议切换到“术语”查询:

{ "from": 0, "size": 100, "query": { "term" : { "name": "John Do"
} } }

might help to avoid the quoting. 可能有助于避免引用。 I have tried this out by use of the @Query annotation on the method findByName in your repository. 我已经通过在存储库中的方法findByName上使用@Query注释来尝试这一点。 It would go something like this: 它会是这样的:

@Query(value = "{\"term\" : {\"name\" : \"?0\"}}")
List<Person> findByName(String name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM