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

标签 regex elasticsearch

我将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/

相关文章:

javascript - 用节点替换其中的大括号和文本

python - 一次读取 1mb 的原始文件,然后在 Python 中执行 Regex

Java 字符串匹配正则表达式

apache-spark - Elasticsearch Spark 解析问题 - 无法解析字段 [Y] 的值 [X]

performance - 优化MLT Elasticsearch查询

regex - Jenkins Git Plugin Build Trigger RegEx 开始错误的工作

php - 全局电子邮件验证

elasticsearch - 由于列中的整数较长,导致grok模式失败

logging - ELK - Logstash + Redis - 数据复制

elasticsearch - 如何对带有/不带有特殊字符的所有类型的单词实现match和match-phrase-prefix的一致行为?