Свойство Geo_Point не индексируется должным образом

Я использую NEST .net клиент для эластичного поиска. У меня есть класс Address с свойством Location.

public class Address
{
public string AddressLine1 {get;set;}
public string AddressLine2 {get;set;}
public string City {get;set;}
public string State {get;set;}
public string ZipCode {get;set;}
[ElasticProperty(Type = FieldType.geo_point)]
public GeoLocation Location {get;set;}
}


public class GeoLocation
{
public float Lat {get;set;}
public float Lon {get;set;}
}

Я украсил свойство Location с помощью атрибута типа ElasticProperty geo_point. Я был в состоянии построить индекс для этого типа. Но когда я пытаюсь получить карту для этого,

http://localhost:9200/mysite/Address/_mapping

я ожидаю, что свойство Location будет иметь тип "geo_point", вместо этого оно показывает что-то вроде этого.

{
    "Address": {
        "properties": {                
            "location": {
                "properties": {
                    "lat": {
                        "type": "double"
                    },
                    "lon": {
                        "type": "double"
                    }
                }
            }
        }
    }
}

Я что-то здесь упускаю?

3 ответа

Вот полный пример того, как отобразить GeoPoint с помощью Nest вместе с настройкой других свойств. Надеюсь, поможет кому-то в будущем.

public class Location
{
    public decimal Lat { get; set; }
    public decimal Lon { get; set; }
}

public class Building : BaseEntity
{
    public IEnumerable<Location> Location { get; set; }
}

public void CreateBuildingMapping()
{
    var nodes = new List<Uri>() { ... };
    var connectionPool = new Elasticsearch.Net.ConnectionPool.SniffingConnectionPool(nodes);
    var connectionSettings = new Nest.ConnectionSettings(connectionPool, "myIndexName");
    var elasticsearchClient = new Nest.ElasticClient(connectionSettings);

    var putMappingDescriptor = new Nest.PutMappingDescriptor<Building>(connectionSettings);
    putMappingDescriptor.DateDetection(false);
    putMappingDescriptor.Dynamic(false);
    putMappingDescriptor.IncludeInAll(true);
    putMappingDescriptor.IgnoreConflicts(false);
    putMappingDescriptor.Index("myIndexName");
    putMappingDescriptor.MapFromAttributes();
    putMappingDescriptor
        .MapFromAttributes()
            .Properties(p =>
                p.GeoPoint(s =>
                    s.Name(n => n.Location).IndexGeoHash().IndexLatLon().GeoHashPrecision(12)
                )
            );
    putMappingDescriptor.NumericDetection(false);

    elasticsearchClient.Map(putMappingDescriptor);
}

Я использовал код Винотса из его комментария.

Пожалуйста, используйте MapFluent() метод для определения отображений. Отображение на основе атрибутов очень ограничено в том, что оно может выразить. В следующем выпуске MapFluent будет переименован в Map, и это будет единственный способ указать отображение.

Смотрите этот пример для отображения geo_point тип:

https://github.com/elasticsearch/elasticsearch-net/blob/master/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs#L225

Создать эластичного клиента

var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).SetDefaultIndex("my_indexone");
var client = new ElasticClient(settings);
client.CreateIndex("my_indexone", s => s.AddMapping<VehicleRecords>(f => f.MapFromAttributes().Properties(p => p.GeoPoint(g => g.Name(n => n.Coordinate ).IndexLatLon()))));

Создать ниже класс

              public class VehicleRecords
                {

                    public string name { get; set; }
                    public Coordinate Coordinate { get; set; }
                    public double Distance { get; set; }
                }

                public class Coordinate
                {
                    public double Lat { get; set; }
                    public double Lon { get; set; }
                }

          Step 2 :Insert some record using above class
          Step 3 :Using below query to search....


 Nest.ISearchResponse<VehicleRecords> Response = client.Search<VehicleRecords>(s => s.Sort(sort => sort.OnField(f => f.Year).Ascending()).From(0).Size(10).Filter(fil => fil.GeoDistance(n => n.Coordinate, d => d.Distance(Convert.ToDouble(100), GeoUnit.Miles).Location(73.1233252, 36.2566525))));
Другие вопросы по тегам