Create Index with GeoPoint type

NEST/Elasticsearch.Net version:5.5

I want to Create index with model like below:
public class company
{

public long id { get; set; }

public string name { get; set; }

public string address { get; set; }

public long areaCode { get; set; }

public string city { get; set; }

public string state { get; set; }

public string latestActivityTimestamp { get; set; }

public location location { get; set; }
}

public class location
{
[JsonProperty(PropertyName = "lat")]
public double latitude { get; set; }

[JsonProperty(PropertyName = "lon")]
public double longitude { get; set; }
}

I want to Create index it and mapping the location as GeoPoint ,I find some example like :
internal class Location
{
[JsonProperty(PropertyName = "lat")]
public double Latitude { get; set; }

[JsonProperty(PropertyName = "lon")]
public double Longitude { get; set; }

}
and then mapping it with:
.MapFromAttributes()
.Properties(p =>
p.GeoPoint(s =>
s.Name(n => n.Location).IndexGeoHash().IndexLatLon().GeoHashPrecision(12)
)
)

The Problem is I can not find MapFromAttributes(), IndexGeoHash() ,I don't know how to deal with my problem.

This is in NEST 1.x, but does not exist in NEST 5.x.

You should use the GeoLocation type as the property to represent a geo_point. Automapping will map this as a geo_point by default.

I import the Geolocation to my project, and define the location as
Geolocation ,but after automapping, I get the mapping is like below

so the type is still not geo_point?

my code:
public class company
{

    public long id { get; set; }

    public string name { get; set; }

    public string address { get; set; }

    public long areaCode { get; set; }

    public string city { get; set; }

    public string state { get; set; }

    public string latestActivityTimestamp { get; set; }

    public GeoLocation location { get; set; }
}

client.CreateIndex(UsersIndexName, descriptor => descriptor
.Mappings(ms => ms
.Map(m => m

.AutoMap()
)
)
);

You'll need to delete the existing index if there is one and create again

public class Message
{
    public GeoLocation Location { get; set; }
}

var defaultIndex = "messages";
var client = new ElasticClient();

if (client.IndexExists(defaultIndex).Exists)
{		
    client.DeleteIndex(defaultIndex);
}
	
client.CreateIndex(defaultIndex, c=> c
    .Mappings(m => m
        .Map<Message>(mm => mm
            .AutoMap()
        )
    )
);

yields

PUT http://localhost:9200/messages
{
  "mappings": {
    "message": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

I already delete the existing index before create again, but it still doesn't work, so I try to use curl to put index and mapping, now the problem is my json data never can bulk in to es , I look at https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html

theirs data I can bulk to es, and they are ndjson, when I edit my data as theirs data ,it can work and bulk to es. like below:
{"index":{"_index":"indexcompany","_type":"company"}}
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}
{"index":{"_index":"indexcompany","_type":"company"}}
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

but my original data is like:
[{
"id": 174,
"name": "JET REPORTS DANMARK ApS",
"address": "Lyskær 3",
"areaCode": "2730",
"city": "Herlev",
"state": null,
"location": {
"latitude": 55.7145620017324,
"longitude": 12.4359915318663
},
"latestActivityTimestamp": null,
"isFavorite": false
}, {
"id": 568,
"name": "OUTOKUMPU A/S",
"address": "Alhambravej 3",
"areaCode": "1826",
"city": "Frederiksberg C",
"state": null,
"location": {
"latitude": 55.6743134573089,
"longitude": 12.5440667455454
},
"latestActivityTimestamp": null,
"isFavorite": false
}]

it never works, so what I can do now?

Run the example provided using NEST 5.5.0 against Elasticsearch 5.x and you'll see that it does work.

You'll either need to convert your data to fit the newline delimited JSON that the bulk API expects, or deserialize your JSON data into C# POCOs and use NEST's Bulk() method to index your data.

Before I always use NEST'S bulk(), but now I have the geopoint type in my data ,the earlier topics I already mentioned it ,I use geolocation to define the location ,but it can't be convert to geopoint type. So I try bulk API and they expects NDJSON, I edit my data like:
{"index":{"_id":"1"}}
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}
{"index":{"_id":"2"}}
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

I get success to import them to es and the location is geopoint type. But if it is necessary every line must with {"index":{"_id":"1"}},{"index":{"_id":"2"}}? I can't bulk them like:
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

Yes, that is the format that the bulk API expects. With NEST, when indexing a collection of objects, the client generates this format for you.

Ok, thank you ,now I understand it ,and I already test some sample data ,get success with geopoint:slight_smile:

About the NEST bulk(), for the geopoint data type it does not work, I use 5.5 version, so If it will be ok in later version? Because we want to import data to ES with C# API.

Can you provide a small concise but complete example demonstrating that it "does not work"?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.