Reputation: 91
I am using ES 2.4.6 with Java 8, and i created a document object as following:
@Document(indexName = "airports", type = "airport")
public class Airport {
@Id
private String id;
@Field(type = String)
private String name;
}
And i successfully search several airport objects to ES, with following names: "San Francisco", "San Mateo", "Santiago", "Palo Alto", "Big San" The JSON content inside ES looks like following:
{
"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"
}
}
]
}
}
Then i have following curl command to use regex query to find all airport names staring with "san" ignoring case, i did:
curl -XGET 'localhost:9200/airports/airport/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"regexp":{
"name": "^(?i)san"
}
}
}
'
I use the regex "^(?i)san" directly match against those airport names, it works as expect:
String regex = "^(?i)san";
assertTrue("San Francisco".matches(regex));
assertTrue("San Mateo".matches(regex));
assertTrue("Santiago".matches(regex));
assertTrue(!"Big San".matches(regex));
So does anyone know why ES regex query returns empty result back? Now, if i use "san" as regex, all 4 names return back, and if i use "San", nothing returns back.
Upvotes: 1
Views: 1769
Reputation: 7649
You can use Match Phrase Prefix for the problem mentioned above.
{
"query": {
"match_phrase_prefix": {
"name": "San"
}
}
}
See if it resolves your problem.
Upvotes: 1