gpt4 book ai didi

regex - ElasticSearch正则表达式查询不起作用

转载 作者:行者123 更新时间:2023-12-03 00:04:06 31 4
gpt4 key购买 nike

我将ES 2.4.6与Java 8配合使用,并且创建了一个文档对象,如下所示:

@Document(indexName = "airports", type = "airport")
public class Airport {

@Id
private String id;

@Field(type = String)
private String name;
}

并且我成功地搜索到ES的几个机场对象,如下
名称: “旧金山”,“圣马特奥”,“圣地亚哥”,“帕洛阿尔托”,“大圣”
ES中的JSON内容如下所示:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "airports",
"_type": "airport",
"_id": "SSMlsTWIYefbXHCnYEwEY",
"_score": 1,
"_source": {
"id": "SSMlsTWIYefbXHCnYEwEY",
"name": "Santiago"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "LlDcKuywPjURNeIISjXLjC",
"_score": 1,
"_source": {
"id": "LlDcKuywPjURNeIISjXLjC",
"name": "San Mateo"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
"_score": 1,
"_source": {
"id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
"name": "San Francisco"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "gbntKR",
"_score": 1,
"_source": {
"id": "gbntKR",
"name": "Palo Alto"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "bKosUdHeseMMboyaejv",
"_score": 1,
"_source": {
"id": "bKosUdHeseMMboyaejv",
"name": "Big San"
}
}
]
}
}

然后我有以下curl命令以使用正则表达式查询来查找所有机场
“san”开头的名字忽略大小写,我这样做了:
curl -XGET 'localhost:9200/airports/airport/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"regexp":{
"name": "^(?i)san"
}
}
}
'

我使用正则表达式“^(?i)san”直接匹配这些机场名称,
它按预期工作:
String regex = "^(?i)san";
assertTrue("San Francisco".matches(regex));
assertTrue("San Mateo".matches(regex));
assertTrue("Santiago".matches(regex));
assertTrue(!"Big San".matches(regex));

那么有人知道为什么ES regex查询返回空结果吗?现在,如果
我使用 “san” 作为正则表达式,所有4个名称都返回,并且如果我使用 “San” ,则没有任何返回。

最佳答案

您可以将Match Phrase Prefix用于上述问题。

 {
"query": {
"match_phrase_prefix": {
"name": "San"
}
}
}

查看是否可以解决您的问题。

关于regex - ElasticSearch正则表达式查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46287641/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com