我有一堆文件如下。我想在projectkey以〜开头的地方过滤数据。
我确实读过一些文章,其中说〜是Elastic查询中的运算符,因此无法真正进行过滤。
有人可以帮助形成/ branch / _search API的搜索查询吗?
{
"_index": "branch",
"_type": "_doc",
"_id": "GAz-inQBJWWbwa_v-l9e",
"_version": 1,
"_score": null,
"_source": {
"branchID": "refs/heads/feature/12345",
"displayID": "feature/12345",
"date": "2020-09-14T05:03:20.137Z",
"projectKey": "~user",
"repoKey": "deploy",
"isDefaultBranch": false,
"eventStatus": "CREATED",
"user": "user"
},
"fields": {
"date": [
"2020-09-14T05:03:20.137Z"
]
},
"highlight": {
"projectKey": [
"~@kibana-highlighted-field@user@/kibana-highlighted-field@"
],
"projectKey.keyword": [
"@kibana-highlighted-field@~user@/kibana-highlighted-field@"
],
"user": [
"@kibana-highlighted-field@user@/kibana-highlighted-field@"
]
},
"sort": [
1600059800137
]
}
更新 ***我在下面使用prerana的答案在查询中使用-prefix
当我使用前缀和范围时,仍然有些问题-我得到以下错误-我缺少了什么?
GET /branch/_search
{
"query": {
"prefix": {
"projectKey": "~"
},
"range": {
"date": {
"gte": "2020-09-14",
"lte": "2020-09-14"
}
}
}
}
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[prefix] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 6,
"col": 5
}
],
"type": "parsing_exception",
"reason": "[prefix] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 6,
"col": 5
},
"status": 400
}
最佳答案
如果我对您的问题很了解,建议您创建一个自定义分析器来搜索特殊字符~
。
在将~
替换为__SPECIAL__
时,我在本地进行了如下测试:
我创建了一个带有自定义char_filter
的索引,并在projectKey
字段中添加了一个字段。新的multi_field的名称为special_characters
。
这是映射:
PUT wildcard-index
{
"settings": {
"analysis": {
"char_filter": {
"special-characters-replacement": {
"type": "mapping",
"mappings": [
"~ => __SPECIAL__"
]
}
},
"analyzer": {
"special-characters-analyzer": {
"tokenizer": "standard",
"char_filter": [
"special-characters-replacement"
]
}
}
}
},
"mappings": {
"properties": {
"projectKey": {
"type": "text",
"fields": {
"special_characters": {
"type": "text",
"analyzer": "special-characters-analyzer"
}
}
}
}
}
}
然后,我在索引中提取了以下内容:“projectKey”:“content1〜”
“projectKey”:“这〜是一个内容”
“projectKey”:“〜路上的汽车”
“projectKey”:“o〜ngram”
然后,查询为:
GET wildcard-index/_search
{
"query": {
"match": {
"projectKey.special_characters": "~"
}
}
}
响应是:"hits" : [
{
"_index" : "wildcard-index",
"_type" : "_doc",
"_id" : "h1hKmHQBowpsxTkFD9IR",
"_score" : 0.43250346,
"_source" : {
"projectKey" : "content1 ~"
}
},
{
"_index" : "wildcard-index",
"_type" : "_doc",
"_id" : "iFhKmHQBowpsxTkFFNL5",
"_score" : 0.3034693,
"_source" : {
"projectKey" : "This ~ is a content"
}
},
{
"_index" : "wildcard-index",
"_type" : "_doc",
"_id" : "-lhKmHQBowpsxTkFG9Kg",
"_score" : 0.3034693,
"_source" : {
"projectKey" : "~ cars on the road"
}
}
]
请让我知道如果您有任何问题,我们将很高兴为您提供帮助。注意:如果
~
后面有空格,则此方法有效。从响应中您可以看到没有显示第四个数据。