I’m facing an issue with retrieving documents using a prefix query in Elasticsearch. Here’s the scenario:
I have documents stored in an index with a field named asset_number
. When I execute the following query:
GET /search_index/_search
{
"query": {
"match_phrase_prefix": {
"asset_number": {
"query": "PC"
}
}
}
}
I expect to retrieve documents where the asset_number field starts with “PC”. However, I’m not getting any results, even though I have documents with asset numbers like “PC1234”, “PC5678”, etc.
Interestingly, when I modify the query to include an additional character in the prefix:
GET /search_index/_search
{
"query": {
"match_phrase_prefix": {
"asset_number": {
"query": "PC1"
}
}
}
}
I can successfully retrieve documents with asset numbers like “PC1234”.
Furthermore, if I create another document with AK1234 as the asset_number, I can retrieve it using a similar query:
GET /search_index/_search
{
"query": {
"match_phrase_prefix": {
"asset_number": {
"query": "AK"
}
}
}
}
Can anyone provide insight of the possible reason why the prefix query behaves differently for “PC” and “AK” in OpenSearch, and why adding an extra character in the prefix affects the results?
2