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

时间:2017-09-18 20:24:09

标签: 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,并跟随 名称:" San Francisco"," San Mateo"," Santiago"," Palo Alto"," Big San& #34; 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正则表达式查询返回空结果?现在,如果 我使用" san" 作为正则表达式,所有4个名称都返回,如果我使用" San" ,则不会返回任何内容。

1 个答案:

答案 0 :(得分:0)

您可以使用Match Phrase Prefix解决上述问题。

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

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