Skip to content

GeoDistanceQuery throws "The given key 'Text' was not present in the dictionary" or "Unable to locate factory for multi-item union accepting a string value" #8513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tudordumitriu opened this issue May 7, 2025 · 4 comments
Labels
8.x Relates to a 8.x client version Category: Bug

Comments

@tudordumitriu
Copy link

tudordumitriu commented May 7, 2025

Elastic.Clients.Elasticsearch version**: 8.18.0

Elasticsearch version: 8.16.0

.NET runtime version: 8.0.15

Operating system version: Windows 11

Description of the problem including expected versus actual behavior:
We are trying to execute a GeoDistanceQuery against a geo_point that has data stored as text in the following format
"25.76,-80.36"
The 2 errors are generated from trying to set the query location parameter from GeoLocation.Text(location) or
GeoLocation.LatitudeLongitude(new LatLonGeoLocation { Lat = lat, Lon = lon })
So any attempt results in failure.

Side notes:
We have even tried upgrading to 9.0.1 but we ran into #8512 but the good news is that with the proper code adjustments the generated query from debug view runs just fine in Postman (so only the header is stopping it from completing successfully)

Steps to reproduce:
1.Create geofilter

new GeoDistanceQuery
                {
                    Field = "location_geo_point",
                    Distance = request.ProximityDistance, //"100km"
                    Location = geoLocation, //as per above mention
                }

2.Set filter to query
3. Executed query
4. Exception thrown: "The given key 'Text' was not present in the dictionary" or "Unable to locate factory for multi-item union accepting a string value"

Expected behavior
Getting a filtered result since the the filter is applied only when there is a proximity search parameter set

Query finalQuery = null;
if (geoFilter != null)
{
    finalQuery = new BoolQuery
    {
        Must = new List<Query> { functionScoreQuery },
        Filter = new List<Query> { geoFilter }
    };
}
else
{
    finalQuery = functionScoreQuery;
}
@tudordumitriu tudordumitriu added 8.x Relates to a 8.x client version Category: Bug labels May 7, 2025
@tudordumitriu tudordumitriu changed the title GeoDistanceQuery throws "The given key 'Text' was not present in the dictionary" or "Unable to locate factory for multi-item union accepting a string value"" GeoDistanceQuery throws "The given key 'Text' was not present in the dictionary" or "Unable to locate factory for multi-item union accepting a string value" May 7, 2025
@tudordumitriu
Copy link
Author

tudordumitriu commented May 7, 2025

Hi once again
I have upgraded both the client and server to 9.0.x and now I am getting the following error
"Error occurred while searching vectors in Elasticsearch: Operation is not valid due to the current state of the object.
Elastic.Transport.UnexpectedTransportException: Operation is not valid due to the current state of the object.
---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Elastic.Clients.Elasticsearch.GeoLocationConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in /_/src/Elastic.Clients.Elasticsearch/_Shared/Types/GeoLocation.cs:line 26"

Here is my CreateGeoLocation function

private GeoLocation CreateGeoLocation(string location) { if (string.IsNullOrEmpty(location)) return null; //return GeoLocation.Text(location); // Try to parse as coordinates (lat,lon) var parts = location.Split(','); if (parts.Length == 2 && double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out double lat) && double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out double lon)) { return GeoLocation.LatitudeLongitude(new LatLonGeoLocation { Lat = lat, Lon = lon }); } // If not coordinates, return null as we can't use text-based locations with GeoDistanceQuery return null; }
Am I missing something completely here?

@flobernd
Copy link
Member

flobernd commented May 8, 2025

Hi @tudordumitriu,

Regarding 8.x:

It seems like the object is not correctly serialized. This will be fixed in 8.19 since I plan to backport all 9.0 changes to the 8.x branch with that version.

Regarding 9.x:

Are you trying to manually deserialize the GeoLocation somewhere in your code? Or do you use that type in your documents/POCO types?

GeoLocation currently only supports serialization.

Edit: It looks like GeoLocation must support serialization since it's used in certain responses (e.g. SearchResponse, if a GeoDistanceAggregation is used).

Will be fixed in 9.0.2 (and backported to 8.19 once that's available).

@tudordumitriu
Copy link
Author

Hi @flobernd
Just found the issue and indeed was the DEserialization problem, but it was quite hard to find
To conclude the problem in the 9.0.x version was that once the result set returned by the server it was crashing while being deserialized and unfortunately found that deserialization was not implemented (as you've just confirmed)

For now just commented out the serialization attribute, because we don't need the field and it works.
But looking forward to 9.0.2 to get it fixed.

Even though in our case the field was just a string, so don't quite get it why would it be converted internally to GeoLocation when there is no explicit conversion nor declaration
//[JsonPropertyName("location_geo")] public string LocationGeo { get; set; }

@flobernd
Copy link
Member

flobernd commented May 8, 2025

Hi @tudordumitriu , thanks for reporting back to this issue.

Even though in our case the field was just a string, so don't quite get it why would it be converted internally to GeoLocation when there is no explicit conversion nor declaration

This sounds really weird. Would it be ok for you to share that part of your document type and the exact query code you use to retrieve the results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.x Relates to a 8.x client version Category: Bug
Projects
None yet
Development

No branches or pull requests

2 participants