Commit a50750dd authored by Yiğit Yusuf Çılgı's avatar Yiğit Yusuf Çılgı
Browse files

Initial commit

parents
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
public class LocationContext : DbContext
{
public DbSet<Cm_CustomerLocations> CustomerLocations { get; set; }
public DbSet<Cm_Customer> Customers { get; set; }
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
<PackageReference Include="NEST" Version="6.5.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Cm_Customer", Schema = "dbo")]
public class Cm_Customer
{
public Cm_Customer()
{
CmCustomerLocations = new HashSet<Cm_CustomerLocations>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Cm_CustomerLocations> CmCustomerLocations { get; set; }
}
\ No newline at end of file
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Cm_CustomerLocations", Schema = "dbo")]
public class Cm_CustomerLocations
{
public Cm_CustomerLocations()
{
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int CustomerId { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public bool IsDeleted { get; set; }
public virtual Cm_Customer Customer { get; set; }
}
\ No newline at end of file
using Nest;
public class CustomerModel
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public int LocationId { get; set; }
public string LocationName { get; set; }
public GeoLocation Location { get; set; }
}
public class Message
{
public GeoLocation Location { get; set; }
}
\ No newline at end of file
using System;
using System.Data;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Nest;
using System.Data.SqlClient;
namespace ElasticService
{
class Program
{
private static readonly ConnectionSettings connSettings =
new ConnectionSettings(new Uri("http://localhost:9200/"))
.DefaultIndex("customerlocation")
.DefaultMappingFor<CustomerModel>(m => m
.IndexName("customerlocation")
);
private static readonly ElasticClient elasticClient = new ElasticClient(connSettings);
// private static IDbConnection _db = new SqlConnection("");
static void Main(string[] args)
{
string sqlString = "Data Source=DESKTOP-MD3HK8G;Initial Catalog=Customer;User ID=root;Password=root;";
string qString = "select cs.Name,c1.Latitude,c1.Longitude from Cm_Customer as cs inner join [dbo].[Cm_CustomerLocations] as c1 on cs.id = c1.id";
SqlConnection connection = new SqlConnection(sqlString);
SqlCommand command = connection.CreateCommand();
command.CommandText = qString;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0)+ " " +reader.GetFloat(1)+ " " +reader.GetFloat(2));
}
reader.Close();
connection.Close();
//Indexing();
// Search();
}
public static void Search()
{
var geoResult = elasticClient.Search<CustomerModel>(s => s.From(0).Size(10000).Query(query => query.Bool(b => b.Filter(filter => filter
.GeoDistance(geo => geo
.Field(f => f.Location)
.Distance("250km").Location(41, 28)
.DistanceType(GeoDistanceType.Plane)
))
)));
foreach (var customer in geoResult.Documents)
{
Console.WriteLine(customer.CustomerName + ":" + customer.LocationName + " = " + GetDistanceFromLatLonInKm(41, 28, customer.Location.Latitude, customer.Location.Longitude).ToString() + "km");
}
Console.ReadLine();
}
public static void Indexing()
{
using (LocationContext dbContext = new LocationContext())
{
var customerLocationList = dbContext.CustomerLocations.Where(s => !s.IsDeleted)
.Include(s => s.Customer)
.Select(s => new CustomerModel
{
CustomerId = s.CustomerId,
CustomerName = s.Customer.Name,
LocationId = s.Id,
LocationName = s.Name,
Location = new GeoLocation(Convert.ToDouble(s.Latitude), Convert.ToDouble(s.Longitude))
}).ToList();
var defaultIndex = "customerlocation";
var client = new ElasticClient();
if (client.IndexExists(defaultIndex).Exists)
{
client.DeleteIndex(defaultIndex);
}
if (!elasticClient.IndexExists("location_alias").Exists)
{
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<CustomerModel>(mm => mm
.AutoMap()
)
).Aliases(a => a.Alias("location_alias"))
);
}
// Insert Data Classic
// for (int i = 0; i < customerLocationList.Count; i++)
// {
// var item = customerLocationList[i];
// elasticClient.Index<CustomerModel>(item, idx => idx.Index("customerlocation").Id(item.LocationId));
// }
// Bulk Insert
var bulkIndexer = new BulkDescriptor();
foreach (var document in customerLocationList)
{
bulkIndexer.Index<CustomerModel>(i => i
.Document(document)
.Id(document.LocationId)
.Index("customerlocation"));
}
elasticClient.Bulk(bulkIndexer);
}
}
public static double GetDistanceFromLatLonInKm(double lat1, double lon1, double lat2, double lon2)
{
var R = 6371; //km cinsinden
var dLat = deg2rad(lat2 - lat1);
var dLon = deg2rad(lon2 - lon1);
var a =
Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
;
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c; // km cinsinden uzaklık
return d;
}
public static double deg2rad(double deg)
{
return deg * (Math.PI / 180);
}
}
}
This diff is collapsed.
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\Yigit Yusuf\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\Yigit Yusuf\\.nuget\\packages",
"C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
]
}
}
\ No newline at end of file
{
"runtimeOptions": {
"tfm": "netcoreapp2.2",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "2.2.0"
}
}
}
\ No newline at end of file
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v2.2", FrameworkDisplayName = "")]
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("ElasticService")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("ElasticService")]
[assembly: System.Reflection.AssemblyTitleAttribute("ElasticService")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
is_global = true
build_property.RootNamespace = ElasticService
build_property.ProjectDir = C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csprojAssemblyReference.cache
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csproj.CoreCompileInputs.cache
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfoInputs.cache
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfo.cs
C:/Source/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.deps.json
C:/Source/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.json
C:/Source/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.dev.json
C:/Source/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.dll
C:/Source/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.pdb
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.dll
C:/Source/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.pdb
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.deps.json
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.json
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.dev.json
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.dll
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.pdb
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csprojAssemblyReference.cache
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csproj.CoreCompileInputs.cache
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfoInputs.cache
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfo.cs
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.dll
/Users/borakasmer/Projects/ElasticSearchGeoLocationExample/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.pdb
/Users/borakasmer/Desktop/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.deps.json
/Users/borakasmer/Desktop/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.json
/Users/borakasmer/Desktop/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.runtimeconfig.dev.json
/Users/borakasmer/Desktop/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.dll
/Users/borakasmer/Desktop/ElasticService/bin/Debug/netcoreapp2.2/ElasticService.pdb
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csprojAssemblyReference.cache
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.csproj.CoreCompileInputs.cache
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfoInputs.cache
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.AssemblyInfo.cs
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.dll
/Users/borakasmer/Desktop/ElasticService/obj/Debug/netcoreapp2.2/ElasticService.pdb
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\bin\Debug\netcoreapp2.2\ElasticService.deps.json
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\bin\Debug\netcoreapp2.2\ElasticService.runtimeconfig.json
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\bin\Debug\netcoreapp2.2\ElasticService.runtimeconfig.dev.json
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\bin\Debug\netcoreapp2.2\ElasticService.dll
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\bin\Debug\netcoreapp2.2\ElasticService.pdb
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.csproj.AssemblyReference.cache
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.GeneratedMSBuildEditorConfig.editorconfig
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.AssemblyInfoInputs.cache
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.AssemblyInfo.cs
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.csproj.CoreCompileInputs.cache
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.dll
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.pdb
C:\Users\Yigit Yusuf\Desktop\ElasticSearchGeolocation-master\obj\Debug\netcoreapp2.2\ElasticService.genruntimeconfig.cache
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment