Esempio n. 1
0
        public async Task <IActionResult> Create([FromBody] RequstSearch <User, User> requstSearch)
        {
            try
            {
                var Check = await _search.Check(requstSearch.Query, requstSearch.Model, null, null,
                                                new List <Func <QueryContainerDescriptor <User>, QueryContainer> >()
                {
                    sh => sh
                    .Match(c => c
                           .Field(p => p.CustomerName)
                           .Query(requstSearch.Model.CustomerName)
                           .Fuzziness(Fuzziness.Ratio(75)))
                    ,
                    sh => sh
                    .Match(c => c
                           .Field(p => p.Email)
                           .Query(requstSearch.Model.Email)
                           .Fuzziness(Fuzziness.Ratio(100))
                           )
                    ,
                    sh => sh
                    .Match(c => c
                           .Field(p => p.Phone1)
                           .Query(requstSearch.Model.Phone1)
                           .Fuzziness(Fuzziness.Ratio(100)))
                    ,
                    sh => sh
                    .Match(c => c
                           .Field(p => p.Phone2)
                           .Query(requstSearch.Model.Phone2)
                           .Fuzziness(Fuzziness.Ratio(100)))
                }

                                                );

                if (Check.Any())
                {
                    return(BadRequest(Check.ToList()));
                }

                await _search.Add(requstSearch.Model);

                return(Ok(requstSearch.Model));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Get([FromForm] IFormFile File)
        {
            try
            {
                _search.Clear();

                List <string[]> Names = new List <string[]>();
                using (var reader = new System.IO.StreamReader(File.OpenReadStream()))
                {
                    while (!reader.EndOfStream)
                    {
                        Names.Add(reader.ReadLine().Split(','));
                    }
                }

                #region Get Keys and score
                Dictionary <string, double> keys = new Dictionary <string, double>();

                for (int i = 0; i < Names.FirstOrDefault().Count(); i++)
                {
                    keys.Add(Names.FirstOrDefault().ElementAtOrDefault(i), Convert.ToDouble(Names.Skip(1).FirstOrDefault().ElementAtOrDefault(i)));
                }
                #endregion


                #region Add To Elastic
                int  divarr = Names.Count() % 2 == 0 ? Names.Skip(2).Count() / 2 : (Names.Skip(2).Count() / 2) + 1;
                Task s      = Task.Run(() =>
                {
                    for (int i = 2; i < divarr; i++)
                    {
                        _search.Add(_helper.ConvertToDynamic(Names.FirstOrDefault(), Names.ElementAtOrDefault(i)));
                    }
                });
                Task s1 = Task.Run(() =>
                {
                    for (int j = divarr; j < Names.Count(); j++)
                    {
                        _search.Add(_helper.ConvertToDynamic(Names.FirstOrDefault(), Names.ElementAtOrDefault(j)));
                    }
                });

                Task.WaitAll(new[] { s, s1 });
                #endregion


                #region Search

                List <Func <QueryContainerDescriptor <dynamic>, QueryContainer> > Filter = new List <Func <QueryContainerDescriptor <dynamic>, QueryContainer> >();
                ElasticServiceDDL.Model.Result UserSimilar = new ElasticServiceDDL.Model.Result()
                {
                    Header = Names.FirstOrDefault()
                };
                List <ModelInfo> modelInfos = new List <ModelInfo>();

                int j = 0;
                for (int i = 2; i < Names.Count(); i++)
                {
                    for (int countFillter = 0; countFillter < Names.FirstOrDefault().Count(); countFillter++)
                    {
                        Filter.Add(new Func <QueryContainerDescriptor <dynamic>, QueryContainer>(
                                       sh => sh
                                       .Match(c => c
                                              .Field(keys.ElementAtOrDefault(j).Key)
                                              .Query(Names.ElementAtOrDefault(i).ElementAtOrDefault(j++).ToString())
                                              .MinimumShouldMatch(MinimumShouldMatch.Percentage(keys.ElementAtOrDefault(j).Value))
                                              )
                                       ));
                    }
                    j = 0;
                    var Similar = _search.SerTestTT(Filter).Result;

                    if (Similar.Count > 1)
                    {
                        modelInfos.Add(new ModelInfo {
                            Model = _helper.ConvertToDynamic(Names.FirstOrDefault(), Names.ElementAtOrDefault(i), Similar)
                        });
                    }

                    Filter.Clear();
                }

                #endregion

                UserSimilar.Model = modelInfos;

                return(await Task.FromResult(Ok(UserSimilar)));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }