elasticsearch - 我想使用过滤器进行搜索,该过滤器排除与条件或其他条件不匹配的结果:
问题描述
如果我像这样给出它,它的给出结果是期望 { "track_total_hits": true, "query": { "bool": { "must": [ { "bool": { "should": [ { "simple_query_string": { “字段”:[“GECORRIGEERDETOEVOEGING”],“查询”:“ {uer} ”,“analyze_wildcard”:“true”,“default_operator”:“AND”}}],“minimum_should_match”:1}}]}}, “_source”:[“GECORRIGEERDETOEVOEGING”,“INLEGDATUM”],“大小”:30,“来自”:0 }
但是如果我这样给我就会出错
{
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"simple_query_string": {
"fields": [
"GECORRIGEERDETOEVOEGING"
],
"query": "*{uer}*",
"analyze_wildcard": "true",
"default_operator": "AND"
}
},
{
"query_string": {
"default_field": "GECORRIGEERDETOEVOEGING.keyword",
"query": "*{uer}*",
"analyze_wildcard": "true",
"default_operator": "AND"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"_source": [
"GECORRIGEERDETOEVOEGING",
"INLEGDATUM"
],
"size": 30,
"from": 0
}
错误:
"failed_shards" : [
{
"shard" : 0,
"index" : "momsarchival2",
"node" : "BxZPpbudTl-zjT1a2OAGZA",
"reason" : {
"type" : "query_shard_exception",
"reason" : "Failed to parse query [*{uer}*]",
"index_uuid" : "e40ac98FRYKKun3C4BXaNA",
"index" : "momsarchival2",
"caused_by" : {
"type" : "parse_exception",
"reason" : "Cannot parse '*{uer}*': Encountered \" \"}\" \"} \"\" at line 1, column 5.\r\nWas expecting:\r\n \"TO\" ...\r\n ",
"caused_by" : {
"type" : "parse_exception",
"reason" : "Encountered \" \"}\" \"} \"\" at line 1, column 5.\r\nWas expecting:\r\n \"TO\" ...\r\n "
}
}
}
}
]
},
"status" : 400
}
<
解决方案
当存在查询字符串的一部分并且需要被视为普通字符时,您必须转义 lucene 保留字符。所以在字符串中"*{uer}*"
你必须逃避{
和}
。
{
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"simple_query_string": {
"fields": [
"GECORRIGEERDETOEVOEGING"
],
"query": "*\\{uer\\}*",
"analyze_wildcard": "true",
"default_operator": "AND"
}
},
{
"query_string": {
"default_field": "GECORRIGEERDETOEVOEGING.keyword",
"query": "*{uer}*",
"analyze_wildcard": "true",
"default_operator": "AND"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"_source": [
"GECORRIGEERDETOEVOEGING",
"INLEGDATUM"
],
"size": 30,
"from": 0
}
推荐阅读
- mysql - mysql查询返回不正确的值
- python - Django 使用 datetime.date() 作为 DateField 的输入来实现过去的日期输入
- html - HTML5 日期时间本地选择器 日期和时间格式到另一个字段
- javascript - 如何永久更改烧瓶网页中单击按钮的外观?
- swap - 如何在 Clojure 中的映射嵌套向量中更新试剂原子过滤
- tensorflow - TensorFlow 数据集如何处理无法装入服务器内存的大数据?
- c++ - 将类成员函数分配给函数指针
- android - 在 Android 上使用 react-native-spalsh 屏幕和 react-native-navigation
- android - Android TextView setText() vs append() 和 Editable.Factory
- tkinter - 主窗口中出现的对话框