public ResultadoPesquisa <T> BuscarDocumentos <T>(string url, string index, TermoPesquisa termoPesquisa)
        {
            var response = _httpClient.PostAsJsonAsync(new Uri($"{url}{index}/_doc/_search"), new StringContent(JsonConvert.SerializeObject(termoPesquisa))).Result;

            var data = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException($"Response Status Code: {response.StatusCode}\n {data}");
            }

            return(JsonConvert.DeserializeObject <ResultadoPesquisa <T> >(data));
        }
Beispiel #2
0
        private async void Pesquisa(object obj)
        {
            if (string.IsNullOrEmpty(TermoPesquisa) || EstadoSelecionado == null)
            {
                return;
            }
            ResultadoPesquisa.Clear();
            IsOcupado = true;
            if (TermoPesquisa.Length == 14)
            {
                var e = GetEmpresa(TermoPesquisa);
                if (e != null)
                {
                    ResultadoPesquisa.Add(e);
                    IsOcupado = false;
                    return;
                }
            }
            IProgress <string> reportEmpresa = new Progress <string>(AddEmpresa);
            string             uf            = EstadoSelecionado.UF;
            List <string>      termos        = new List <string>(TermoPesquisa.Split(' '));

            termos.RemoveAll(t => t.Length <= 2);
            using (cts = new CancellationTokenSource())
            {
                await Task.Run(() =>
                {
                    var estado = GetEstado(uf);
                    foreach (var municipio in estado.Municipios)
                    {
                        foreach (var empresa in municipio.Empresas)
                        {
                            if (empresa.Contato == null || empresa.Contato.Numeros.Count == 0 && string.IsNullOrEmpty(empresa.Contato.Email))
                            {
                                continue;
                            }
                            //Verifica se todos os termos da pesquisa foram encontrados
                            bool isTermoCompleto = false;
                            foreach (var t in termos)
                            {
                                if (empresa.CNAE_Fiscal.Descricao.Contains(t, StringComparison.CurrentCultureIgnoreCase) ||
                                    empresa.RazaoSocial.Contains(t, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    isTermoCompleto = true;
                                    continue;
                                }
                                isTermoCompleto = false;
                            }
                            if (isTermoCompleto)
                            {
                                reportEmpresa.Report(empresa.CNPJ);
                            }
                            if (cts.IsCancellationRequested)
                            {
                                return;
                            }
                        }
                    }
                });

                IsOcupado = false;
            }
        }