Skip to content

Commit

Permalink
Serach - autocomplate
Browse files Browse the repository at this point in the history
  • Loading branch information
uleus authored and uleus committed Feb 4, 2021
1 parent 4329a8c commit e5f1bf1
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 102 deletions.
59 changes: 26 additions & 33 deletions Elasticsearch/Search.Elasticsearch/Indexing/IndexBaseItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,34 @@ protected static IPromise<IIndexSettings> InitCommonIndexSettingsDescriptor(Inde
return aDescriptor
.NumberOfReplicas(0)
.NumberOfShards(1)
/* .Analysis(InitCommonAnalyzers)*/;
.Analysis(InitCommonAnalyzers);
}

//protected static IAnalysis InitCommonAnalyzers(AnalysisDescriptor analysis)
//{
// return analysis.Analyzers(a => a
// .Custom("html_stripper", cc => cc
// .Filters("eng_stopwords", "trim", "lowercase")
// .CharFilters("html_strip")
// .Tokenizer("autocomplete")
// )
// .Custom("keywords_wo_stopwords", cc => cc
// .Filters("eng_stopwords", "trim", "lowercase")
// .CharFilters("html_strip")
// .Tokenizer("key_tokenizer")
// )
// .Custom("autocomplete", cc => cc
// .Filters("eng_stopwords", "trim", "lowercase")
// .Tokenizer("autocomplete")
// )
// )
// .Tokenizers(tdesc => tdesc
// .Keyword("key_tokenizer", t => t)
// .EdgeNGram("autocomplete", e => e
// .MinGram(3)
// .MaxGram(15)
// .TokenChars(TokenChar.Letter, TokenChar.Digit)
// )
// )
// .TokenFilters(f => f
// .Stop("eng_stopwords", lang => lang
// .StopWords("_english_")
// )
// );
//}
protected static IAnalysis InitCommonAnalyzers(AnalysisDescriptor analysis)
{
return analysis.Analyzers(a => a
.Custom("autocomplete", ca => ca
.Filters("eng_stopwords", "trim", "lowercase")
.Tokenizer("autocomplete")
)
.Custom("autocomplete_search", ca => ca
.Filters("eng_stopwords", "trim", "lowercase")
.Tokenizer("lowercase")
)
)
.Tokenizers(tdesc => tdesc
.EdgeNGram("autocomplete", e => e
.MinGram(3)
.MaxGram(15)
.TokenChars(TokenChar.Letter, TokenChar.Digit)
)
)
.TokenFilters(f => f
.Stop("eng_stopwords", lang => lang
.StopWords("_english_")
)
);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class SearchableBaseItem
[Keyword(Name = nameof(Id))]
public int Id { get; set; }

[Text(/*Analyzer = "autocomplete", */Name = nameof(Name))]
[Text(Analyzer = "autocomplete", SearchAnalyzer = "autocomplete_search", Name = nameof(Name))]
public string Name { get; set; }

[Text(/*Analyzer = "autocomplete", */Name = nameof(Market))]
[Text(/*Analyzer = "autocomplete",*/ Name = nameof(Market))]
public string Market { get; set; }

[Text(/*Analyzer = "autocomplete", */Name = nameof(State))]
[Text(/*Analyzer = "autocomplete",*/ Name = nameof(State))]
public string State { get; set; }

public SearchableBaseItem()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class SearchablePropertyItem : SearchableBaseItem
[Text(/*Analyzer = "autocomplete", */Name = nameof(City))]
public string City { get; set; }

[Text(Name=nameof(Lat))]
public string Lat { get; set; }
//[Text(Name=nameof(Lat))]
public float Lat { get; set; }

[Text(Name = nameof(Lng))]
public string Lng { get; set; }
//[Text(Name = nameof(Lng))]
public float Lng { get; set; }
}
}
35 changes: 21 additions & 14 deletions Elasticsearch/Search.Elasticsearch/Search/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,32 @@ namespace Search.Elasticsearch.Search
{
public interface ISearchService
{
Task<ISearchResponse<SearchableBaseItem>> Search(SimpleSearchRequest aSearchRequest);
Task<SimpleSerachResponse> Search(SimpleSearchRequest aSearchRequest);
}

public class SearchService :
ISearchService
{

private IElasticClient _client;

public SearchService(IElasticClientFactoryService aElasticClientFactoryService)
{
_client = aElasticClientFactoryService.CreateElasticClient();
}

public async Task<ISearchResponse<SearchableBaseItem>> Search(SimpleSearchRequest aSearchRequest)
public async Task<SimpleSerachResponse> Search(SimpleSearchRequest aSearchRequest)
{
BoolQuery filterQuery = new BoolQuery();
if (!string.IsNullOrEmpty(aSearchRequest.Filter))
{
var filterQueryParts = new List<QueryContainer>();
filterQueryParts.Add(new MatchQuery()
{
Field = $"{nameof(SearchableBaseItem.Market)}",
Query = aSearchRequest.Filter
}
filterQueryParts.Add(
new MatchQuery()
{
Field = $"{nameof(SearchableBaseItem.Market)}",
Query = aSearchRequest.Filter.ToLower(),
Fuzziness = Fuzziness.Auto
}
);

filterQuery.Filter = filterQueryParts;
Expand All @@ -41,23 +42,29 @@ public async Task<ISearchResponse<SearchableBaseItem>> Search(SimpleSearchReques
.Size(aSearchRequest.PageSize)
.Skip(aSearchRequest.PageStartIndex)
.Index(Indices.Index(aSearchRequest.Indices))

.Query(q => q
.MultiMatch(m => m
.Query(aSearchRequest.Query)
.Fields(ff => ff
.Query(aSearchRequest.Query.ToLower())
.Fields(ff => ff
.Field($"{nameof(SearchableBaseItem.Name)}")
.Field($"{nameof(SearchableBaseItem.Market)}")
.Field($"{nameof(SearchableBaseItem.State)}")
.Field($"{nameof(SearchablePropertyItem.FormerName)}")
.Field($"{nameof(SearchablePropertyItem.StreetAddres)}")
.Field($"{nameof(SearchablePropertyItem.City)}")
)
)
//.Fuzziness(Fuzziness.Auto)
)
&& filterQuery
)
)
);

return results;
}
return new SimpleSerachResponse()
{
TotalItems = results.Total,
Items = results.Documents
};
}
}
}
13 changes: 13 additions & 0 deletions Elasticsearch/Search.Elasticsearch/Search/SimpleSerachResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Search.Elasticsearch.Mapping;
using System;
using System.Collections.Generic;
using System.Text;

namespace Search.Elasticsearch.Search
{
public class SimpleSerachResponse
{
public IReadOnlyCollection<SearchableBaseItem> Items { get; set; }
public long TotalItems { get; set; }
}
}
125 changes: 102 additions & 23 deletions Elasticsearch/Search.IndexData.Exe/DataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Newtonsoft.Json;
using Nancy.Json;
using Newtonsoft.Json;
using RestSharp;
using Search.Elasticsearch.Indexing;
using Search.Elasticsearch.Mapping;
using Search.WebAPI.Exe.Dto;
using System;
using System.Collections.Generic;
using System.IO;


namespace Search.IndexData.Exe
{
class DataProvider :
Expand All @@ -14,42 +17,33 @@ class DataProvider :
public List<SearchablePropertyItem> Properties { get; protected set; }
public List<SearchableManagementItem> Managements { get; protected set; }

public DataProvider(string aPropertiesJsonFilePath, string aMgmtJsonFilePath)
{
//List<JsonObject> data = JsonConvert.DeserializeObject<List<JsonObject>>(File.ReadAllText(aPropertiesJsonFilePath), new JsonSerializerSettings()
//{
// Culture = global.C
//});
//new JsonSerializerSettings()
//{
// FloatFormatHandling = FloatFormatHandling.
//});

public DataProvider()
{
this.Properties = new List<SearchablePropertyItem>()
{
new SearchablePropertyItem()
{
Id= 1,
Name ="prop1",
Name ="My granny has a wooden super chair",
StreetAddres = "test",
Market="Warszawa",
State ="a"
Market="Austin",
State ="GS"
},
new SearchablePropertyItem()
{
Id = 2,
Name ="prop2",
StreetAddres = "nowa",
Market="Warszawa",
State ="a"
Market="Austin",
State ="GS"
},
new SearchablePropertyItem()
{
Id = 3,
Name ="prop3",
StreetAddres = "nowa",
Market="Szczecin",
State ="a"
Market="San Francisco",
State ="CA"
},
};

Expand All @@ -58,11 +52,96 @@ public DataProvider(string aPropertiesJsonFilePath, string aMgmtJsonFilePath)
new SearchableManagementItem()
{
Id = 1,
Name = "test",
Market="Szczecin",
State ="a"
Name = "Company A",
Market="San Paulo",
State ="TX"
}
};
};

}

public int LoadManagemetns(string aPath)
{
int iErrorCounter = 0;
var data = System.Text.Json.JsonSerializer.Deserialize<List<ManagementItemWrapper>>(File.ReadAllText(aPath));

foreach (var mw in data)
if (mw.mgmt != null)
this.Managements.Add(
new SearchableManagementItem()
{
Id = mw.mgmt.mgmtID,
Name = mw.mgmt.name,
State = mw.mgmt.state,
Market = mw.mgmt.market
}
);
else
{
iErrorCounter++;
}

return iErrorCounter;
}

public void LoadProperties(string aPath)
{
int iErrorCounter = 0;
var data = System.Text.Json.JsonSerializer.Deserialize<List<PropertyItemWrapper>>(File.ReadAllText(aPath));

foreach (var pw in data)
if (pw.property != null)
this.Properties.Add(
new SearchablePropertyItem()
{
Id = pw.property.propertyID,
Name = pw.property.name,
State = pw.property.state,
Market = pw.property.market,
FormerName = pw.property.formerName,
City = pw.property.city,
StreetAddres = pw.property.streetAddres,
Lat = pw.property.lat,
Lng = pw.property.lng
}
);
else
{
iErrorCounter++;
}


}

class PropertyItemWrapper
{
public PropertyItem property { get; set; }
}

public class PropertyItem
{
public int propertyID { get; set; }
public string name { get; set; }
public string formerName { get; set; }
public string streetAddres { get; set; }
public string city { get; set; }
public string market { get; set; }
public string state { get; set; }
public float lat { get; set; }
public float lng { get; set; }
}

class ManagementItemWrapper
{
public ManagementItem mgmt { get; set; }
}

public class ManagementItem
{
public int mgmtID { get; set; }
public string name { get; set; }
public string market { get; set; }
public string state { get; set; }
}
}
}
Loading

0 comments on commit e5f1bf1

Please sign in to comment.