NEST API Default value for GeoShapes fields

前提是你 提交于 2020-06-17 09:42:07

问题


We are using a filter as per following:

filters.Add(fq => fq
    .Term(t => t
        .Field(f => f.LocalityId)
        .Value(locationParams[2])) || fq
    .GeoShape(g => g
        .Field("locationShape")
        .Relation(GeoShapeRelation.Within)
        .IndexedShape(f => f
            .Id(searchCriteria.spLocationId)
            .Index(indexName)
            .Path("geometry")
        )
    )
);

However, if the geometry field is missing, Elasticsearch throws an exception.

Is there anyway to avoid this by using a default (Null Value) in the mapping or any other way.


回答1:


It is not possible to avoid the exception in this case. Elasticsearch assumes that the parameters that the user provides to a pre-indexed shape are valid.

Ideally, the values supplied to the indexed shape should be constrained in a manner that prevents an end user from supplying invalid values. If that is unfeasible, you could run a bool query with filter clauses of exists query and ids query on the indexName index before adding the indexed shape geoshape query filter to the search request.

For example

private static void Main()
{
    var documentsIndex = "documents";
    var shapesIndex = "shapes";
    var host = "localhost";
    var pool = new SingleNodeConnectionPool(new Uri($"http://{host}:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<Document>(m => m.IndexName(documentsIndex))
        .DefaultMappingFor<Shape>(m => m.IndexName(shapesIndex));

    var client = new ElasticClient(settings);

    if (client.Indices.Exists(documentsIndex).Exists)
        client.Indices.Delete(documentsIndex);

    client.Indices.Create(documentsIndex, c => c
        .Map<Document>(m => m
            .AutoMap()
        )
    );

    if (client.Indices.Exists(shapesIndex).Exists)
        client.Indices.Delete(shapesIndex);

    client.Indices.Create(shapesIndex, c => c
        .Map<Shape>(m => m
            .AutoMap()
        )
    );

    client.Bulk(b => b
        .IndexMany(new [] { 
            new Document 
            {
                Id = 1,
                LocalityId = 1,
                LocationShape = GeoWKTReader.Read("POLYGON ((30 20, 20 15, 20 25, 30 20))")
            },
            new Document
            {
                Id = 2,
                LocalityId = 2
            },
        })
        .IndexMany(new [] 
        {
            new Shape 
            {
                Id = 1,
                Geometry = GeoWKTReader.Read("POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35))")
            }
        })
        .Refresh(Refresh.WaitFor)
    );

    var shapeId = 1;

    var searchResponse = client.Search<Shape>(s => s
        .Size(0)
        .Query(q => +q
            .Ids(i => i.Values(shapeId)) && +q
            .Exists(e => e.Field("geometry"))
        )
    );

    Func<QueryContainerDescriptor<Document>, QueryContainer> geoShapeQuery = q => q;

    if (searchResponse.Total == 1)
        geoShapeQuery = q => +q
            .GeoShape(g => g
                .Field("locationShape")
                .Relation(GeoShapeRelation.Within)
                .IndexedShape(f => f
                    .Id(shapeId)
                    .Index(shapesIndex)
                    .Path("geometry")
                )
            );

    client.Search<Document>(s => s
        .Query(q => +q
            .Term(t => t
                .Field(f => f.LocalityId)
                .Value(2)
            ) || geoShapeQuery(q)
        )
    );
}

public class Document
{
    public int Id { get; set; }
    public int LocalityId { get; set; }
    public IGeoShape LocationShape { get; set; }
}

public class Shape
{
    public int Id { get; set; }
    public IGeoShape Geometry { get; set; }
}

If var shapeId = 1; is changed to var shapeId = 2; then the geoshape query is not added to the filter clauses when searching on the documents index.



来源:https://stackoverflow.com/questions/61913118/nest-api-default-value-for-geoshapes-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!