簡體   English   中英

ElasticSearch正則表達式查詢不起作用

[英]ElasticSearch regex query doesn't work

我將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” 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” ,則沒有任何返回。

您可以為上述問題使用匹配詞組前綴

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

查看它是否可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM