Exemplo n.º 1
3
 public static List<NewsBase> SearchList(string nid)
 {
     string espath = ConfigurationManager.AppSettings["ESPath"].ToString();
     string indexname = ConfigurationManager.AppSettings["IndexName"].ToString();
     string typename = ConfigurationManager.AppSettings["TypeName"].ToString();
     var node = new Uri(espath);
     var settings = new ConnectionSettings(node);
     var client = new ElasticClient(settings);
     //keyword = String.Format("*{0}*", keyword);
     var searchResults = client.Search<NewsBase>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(q => q.QueryString(qs => qs.Query(nid).DefaultOperator(Operator.And)))
         .Sort(st => st.OnField(f => f.newsid).Order(SortOrder.Descending))  /*按ID排序,id为数字,排序正常*/
         //.Sort(st=>st.OnField(f=>f.PubDate.Suffix("sort")).Descending())   /*按时间排序,时间格式,排序bug;中文字符串bug*/
         .From(0)
         .Size(1)
     );
     List<NewsBase> eslist = new List<NewsBase>(searchResults.Documents);
     //foreach (var data in searchResults.Documents)
     //{
     //    eslist.Add(data);
     //}
     return eslist;
 }
Exemplo n.º 2
0
        private IEnumerable <Dotace> YieldAll(QueryContainer query,
                                              string scrollTimeout = "2m",
                                              int scrollSize       = 300)
        {
            ISearchResponse <Dotace> initialResponse = null;

            if (query is null)
            {
                initialResponse = _esClient.Search <Dotace>(scr => scr
                                                            .From(0)
                                                            .Take(scrollSize)
                                                            .MatchAll()
                                                            .Scroll(scrollTimeout));
            }
            else
            {
                initialResponse = _esClient.Search <Dotace>(scr => scr
                                                            .From(0)
                                                            .Take(scrollSize)
                                                            .Query(q => query)
                                                            .Scroll(scrollTimeout));
            }

            if (!initialResponse.IsValid || string.IsNullOrEmpty(initialResponse.ScrollId))
            {
                throw new Exception(initialResponse.ServerError.Error.Reason);
            }

            if (initialResponse.Documents.Any())
            {
                foreach (var dotace in initialResponse.Documents)
                {
                    yield return(dotace);
                }
            }

            string scrollid           = initialResponse.ScrollId;
            bool   isScrollSetHasData = true;

            while (isScrollSetHasData)
            {
                ISearchResponse <Dotace> loopingResponse = _esClient.Scroll <Dotace>(scrollTimeout, scrollid);
                if (loopingResponse.IsValid)
                {
                    foreach (var dotace in loopingResponse.Documents)
                    {
                        yield return(dotace);
                    }
                    scrollid = loopingResponse.ScrollId;
                }
                isScrollSetHasData = loopingResponse.Documents.Any();
            }

            _esClient.ClearScroll(new ClearScrollRequest(scrollid));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            elasticSettings = new ConnectionSettings(new Uri("http://127.0.0.1:9200"))
            .SetDefaultIndex("people");

            client = new ElasticClient(elasticSettings);

            client.DeleteIndex("people");

            // Create an index
            client.CreateIndex("people", c => c
                                                  .NumberOfReplicas(0)
                                                  .NumberOfShards(1));

            client.MapFluent<Person>(m => m.IndexAnalyzer("standard")
                                                        .Properties(props => props
                                                            .String(s => s
                                                                .Name(p => p.Message)
                                                                .Index(FieldIndexOption.analyzed))
                                                            .Number(n => n
                                                                .Name(p => p.Age))
                                                            .String(s => s
                                                                .Name(p => p.FirstName))
                                                            .String(s => s
                                                                .Name(p => p.Sex))
                                                            .String(s => s
                                                                .Name(p => p.LastName))));

            //Add some people
            var jp = new Person { FirstName = "JP", LastName = "Smith", Age = 37, Message = "OMG yay ES!", Sex = "Male" };
            var matt = new Person { FirstName = "Matt", LastName = "Toto", Age = 32, Message = "I'm JPs brother", Sex = "Male" };
            var christine = new Person { FirstName = "Christine", LastName = "Toto", Age = 0, Message = "I'm JPs wife", Sex = "Female" };
            var kevin = new Person { FirstName = "Kevin", LastName = "Toto", Age = 26, Message = "I'm JPs other brother", Sex = "Male" };

            client.Index(jp);
            client.Index(matt);
            client.Index(christine);
            client.Index(kevin);

            client.Refresh();

            ///// ** Wildcard search
            var searchResults = client.Search<Person>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                    .Wildcard(f => f.LastName, "to*", Boost: 1.0)
                )
            );

            foreach (var result in searchResults.Documents)
            {
                Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            }

            Console.ReadKey();
        }
        internal static IQueryable<UAutoContractJournalItem> GetJournalItemsFromElastic(Uri node, SearchRequest request)
        {
            var settings = new ConnectionSettings(
                node,
                defaultIndex: "uauto");

            var client = new ElasticClient(settings);

            var searchResult = client.Search<UAutoContractJournalItem>(request);

            return searchResult.Documents.AsQueryable();
        }
Exemplo n.º 5
0
        // GET: Home
        public ActionResult Index()
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(connectionPool)
                           .DefaultIndex("product")
                           .DisableDirectStreaming()
                           .OnRequestCompleted(RequestHandler.OnRequestCompletedHandler);
            var client = new Nest.ElasticClient(settings);

            ISearchResponse <EsDocument> data = client.Search <EsDocument>(s => s.Query(q => q.MatchAll()));

            return(View(data.Documents));
        }
        public JsonResult Search(string term)
        {
            var uri = new Uri("https://IlwEAOgvDkuHk3yiB74RhwSs1YC0KCUu:@aniknaemm.east-us.azr.facetflow.io");
            var settings = new ConnectionSettings(uri).SetDefaultIndex("indexdem");
            var client = new ElasticClient(settings);

            var result = client.Search<Demotivator>(q => q
            .Query(f => f
               .QueryString(t => t.Query(term + "*").OnFields(u => u.Name)) || f
               .QueryString(t => t.Query(term + "*").OnFields(u => u.Str1)))

            );

            return Json(result.Hits.Select(t => t.Source), JsonRequestBehavior.AllowGet);
        }
        public IHttpActionResult PerformSearch([FromUri] string indexName, 
            [FromUri] string searchTerm)
        {
            SampleConfiguration config = new SampleConfiguration();
            esNode = new Uri(config.ElasticsearchServerHost);
            connectionSettings = new ConnectionSettings(esNode, defaultIndex: indexName);
            esClient = new ElasticClient(connectionSettings);

            var esResults = esClient.Search<Product>(s => s
                .Query(p => p.MatchPhrase(m => m.OnField("name").Query(searchTerm)))
                //.Filter(f => f.Range(r => r.OnField("price").GreaterOrEquals(10).LowerOrEquals(20)))
                );

            return Ok(esResults.Documents);
        }
Exemplo n.º 8
0
        public IEnumerable<Food> SearchFood(string Name)
        {
            var EsClient = new ElasticClient();

            var ES_Search = EsClient.Search<Food>(s => s
            .Index("food")
            .Type(Name)
            .From(0)
            .Size(10)
            .Query(q => q
                        .QueryString(ps => ps
                        .DefaultField(o => o.Name)

            )));
            return ES_Search.Documents;
        }
Exemplo n.º 9
0
        public void SearchDoesntLowercaseStringFieldOverload()
        {
            var settings = new ConnectionSettings(Test.Default.Uri).SetDefaultIndex(Test.Default.DefaultIndex);
            var client = new ElasticClient(settings);
            var result = client.Search<SomeOtherClass>(s => s
              .SortDescending("CreateDate2")
              .FacetDateHistogram("CreateDate2", fd => fd.OnField("CreateDate2").Interval(DateInterval.Hour))
              .Query(query => query.Range(r=>r
                  .OnField("CreateDate2")
                  .From(DateTime.UtcNow.AddYears(-1))

                )
              )
            );
            StringAssert.DoesNotContain("createDate2", result.ConnectionStatus.Request);
        }
Exemplo n.º 10
0
 public IHttpActionResult GetMessage(int id)
 {
     //var message = messages.FirstOrDefault((m) => m.Id == id);
     client = this.Connect();
     var results = client.Search<Message>(s => s
                                             .From(0)
                                             .Size(10)
                                             .Query(q => q
                                                  .Term(m => m.Id, id)
         )
     );
     var message = results.Documents.FirstOrDefault();
     if (message == null)
     {
         return NotFound();
     }
     return Ok(message);
 }
Exemplo n.º 11
0
        private static void ElasticSearchTest()
        {
            var node = new Uri("http://localhost:9200");

            var settings = new ConnectionSettings(
                node,
                defaultIndex: "my-application"
            );

            var client = new ElasticClient(settings);

            Product product = new Product("Tv", DateTime.Now, 200, new string[] { "45''", "48''" });

            var index = client.Index(product);

            ISearchResponse<Product> searchResults = client.Search<Product>(s => s.MatchAll());
            Product prd = searchResults.Hits.FirstOrDefault().Source;
        }
 public void SearchUsesTheProperResolver()
 {
     var settings = new ConnectionSettings(new Uri("http://localhost:9200")).SetDefaultIndex(Test.Default.DefaultIndex);
       var client = new ElasticClient(settings);
       var result = client.Search<SomeOtherClass>(s => s
     .SortDescending(f=>f.MyCustomOtherClass.MyProperty)
     .Query(query => query
       .Bool(bq => bq
     .Must(
       mq => mq.ConstantScore(cs => cs.Filter(filter => filter.Term(x => x.MyCustomClass.MyProperty, "meesageid"))),
       mp => mp.ConstantScore(cs => cs.Filter(filter => filter.Term(x => x.MyCustomOtherClass.MyProperty, "serverid")))
     )
       )
     )
       );
       StringAssert.Contains("custom.MID", result.ConnectionStatus.Request);
       StringAssert.Contains("myCustomOtherClass.MID", result.ConnectionStatus.Request);
 }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            elasticSettings = new ConnectionSettings(new Uri("http://127.0.0.1:9200"))
                .SetDefaultIndex("people");

            client = new ElasticClient(elasticSettings);

            client.DeleteIndex("people");

            // Create an index
            client.CreateIndex("people", c => c
                                    .NumberOfReplicas(0)
                                    .NumberOfShards(1)
                                    .AddMapping<Person>(m => m.MapFromAttributes()));

            // Add some people
            var jp = new Person { FirstName = "JP", LastName = "Toto", Age = 37, Message = "OMG yay ES!", Sex = "Male" };
            var matt = new Person { FirstName = "Matt", LastName = "Toto", Age = 32, Message = "I'm JPs brother", Sex = "Male" };
            var christine = new Person { FirstName = "Christine", LastName = "Toto", Age = 0, Message = "I'm JPs wife", Sex = "Female" };
            var kevin = new Person { FirstName = "Kevin", LastName = "Toto", Age = 26, Message = "I'm JPs other brother", Sex = "Male" };

            client.Index(jp);
            client.Index(matt);
            client.Index(christine);
            client.Index(kevin);

            client.Refresh();

            var searchResults = client.Search<Person>(s => s
                .From(0)
                .Size(10)
                .Fields(f => f.FirstName, f => f.LastName)
                .Query(q => q
                    .MatchPhrase(j => j.OnField("Message").QueryString("i'm jps brother"))
                )
            );

            foreach (var result in searchResults.Documents)
            {
                Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            }

            Console.ReadKey();
        }
Exemplo n.º 14
0
        public void elasticSearch() {


            var uri = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(uri).SetDefaultIndex("contacts");
            var client = new ElasticClient(settings);

            if (client.IndexExists("contacts").Exists)
            {

                UpsertContact(client, new Contacts(1,"Andrew Kagwa", "Uganda"));
                Console.WriteLine("Indexing Successfull");
            }
            else
            {

            }



            Console.ReadKey();

            QueryContainer query = new TermQuery
            {
                Field = "Name",
                Value = "Andrew",
                 
                
            };

            var searchReq = new SearchRequest
            {

                From = 0,
                Size = 10,
                Query = query,
            };

            var result = client.Search<Contacts>(searchReq);



        }
        public void CreatePersonWithImageAttachment()
        {
            var node = new Uri("http://localhost:9200");

            var settings = new ConnectionSettings(
                node,
                defaultIndex: "my-application"
            );

            var client = new ElasticClient(settings);
            //client.DeleteIndex("index-name");
            client.CreateIndex("index-name", c => c
                 .AddMapping<Document>(m => m.MapFromAttributes())
              );

            string path = "c:\\test.png";

            var attachment = new Attachment
            {
                Content = Convert.ToBase64String(File.ReadAllBytes(path)),
                ContentType = "image/jpeg",
                Name = "IMG_9896.JPG"
            };

            var person = new Person
            {
                Id = Convert.ToString(new Random(DateTime.Now.Second).Next(20000)),
                Firstname = "Martijn",
                Lastname = "Laarman",
                File = new[] { attachment }
            };

            var index = client.Index(person);

            var results = client.Search<Person>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                     .Term(p => p.Firstname, "martijn")
                )
            );
        }
Exemplo n.º 16
0
        public static IEnumerable <OsobaES> YieldAllPoliticians(string scrollTimeout = "2m", int scrollSize = 1000)
        {
            ISearchResponse <OsobaES> initialResponse = _esClient.Search <OsobaES>
                                                            (scr => scr.From(0)
                                                            .Take(scrollSize)
                                                            .Query(_query => _query.Term(_field => _field.Status, (int)Osoba.StatusOsobyEnum.Politik))
                                                            .Scroll(scrollTimeout));

            if (!initialResponse.IsValid || string.IsNullOrEmpty(initialResponse.ScrollId))
            {
                throw new Exception(initialResponse.ServerError.Error.Reason);
            }

            if (initialResponse.Documents.Any())
            {
                foreach (var osoba in initialResponse.Documents)
                {
                    yield return(osoba);
                }
            }

            string scrollid           = initialResponse.ScrollId;
            bool   isScrollSetHasData = true;

            while (isScrollSetHasData)
            {
                ISearchResponse <OsobaES> loopingResponse = _esClient.Scroll <OsobaES>(scrollTimeout, scrollid);
                if (loopingResponse.IsValid)
                {
                    foreach (var osoba in loopingResponse.Documents)
                    {
                        yield return(osoba);
                    }
                    scrollid = loopingResponse.ScrollId;
                }
                isScrollSetHasData = loopingResponse.Documents.Any();
            }

            _esClient.ClearScroll(new ClearScrollRequest(scrollid));
        }
        public List<ClientInformation> GetClientsBy(string name, bool? onlyPossiblyStolen)
        {
            ElasticClient esClient;
                        
            var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
            settings.SetDefaultIndex(INDEX);

            esClient = new ElasticClient(settings);

            //text is always in lowercase in ES
            if (!string.IsNullOrEmpty(name))
                name = name.ToLowerInvariant();

            var result = esClient.Search<ClientInformation>(
               sd => sd.Query(  q=> q.Strict(false).Wildcard(t => t.Name,name))
                   .Filter(f => 
                       f.Strict(false).Term(t => t.PossiblyStolen, onlyPossiblyStolen))                  
                   
                   );

            return result.Documents.ToList();
        }
Exemplo n.º 18
0
        public IEnumerable<NearestNoteModel> GetNearest(string userId, double latitude, double longitude, int distance)
        {
            var settings = new ConnectionSettings(new Uri("http://localhost:9200"), "mapnotes");
            var client = new ElasticClient(settings);

            var result = client.Search<NoteIndex>(s => s
                .Take(9999)
                .Index("mapnotes")
                .Query(q => q.Match(m => m.OnField(f => f.UserId).Query(userId)))
                .Filter(f => f.GeoDistance(l => l.Location, d => d.Distance(distance, GeoUnit.Kilometers)
                                    .Location(latitude, longitude)
                                    .Optimize(GeoOptimizeBBox.Indexed))));

            var notes = result.Documents.Select(x => new NearestNoteModel
            {
                Title = x.Title,
                Latitude = x.Location.Lat,
                Longitude = x.Location.Lon
            });

            return notes;
        }
Exemplo n.º 19
0
        public void FieldsSelectionIsCovariantAsWell()
        {
            var settings = new ConnectionSettings(Test.Default.Uri, "default-index")
                .ExposeRawResponse();
            var file = this.GetFileFromMethod(MethodInfo.GetCurrentMethod(), "FixedCovariantSearchResult");
            var jsonResponse = File.ReadAllText(file);
            var connection = new InMemoryConnection(this._settings, jsonResponse);
            var client = new ElasticClient(settings, connection);

            var results = client.Search<BaseClass>(s => s
                .Types(typeof(ClassA),typeof(ClassB),typeof(ClassC),typeof(ClassD))
                .Fields(p=>p.Lang)
            );

            results.Total.Should().Be(1605);

            results.Hits.Should().NotBeNull().And.HaveCount(10);
            var classAHits = results.Hits.OfType<Hit<ClassA>>();
            classAHits.Should().NotBeNull().And.HaveCount(3);

            var classAHit = classAHits.First();
            classAHit.Fields.Should().NotBeNull();
            var lang = classAHit.Fields.FieldValue<ClassA, string>(p => p.Lang).FirstOrDefault();
        }
Exemplo n.º 20
0
        public static List<ResumoConsultaDetalhada> ConsultaResumoConsulta(string paramatro, string ncm)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            var filterQuery2 = Query<PRODUTOS_SENSIVEIS_POCO>.Terms("ncm", ncm.ToLower());
            var result = client.Search<ResumoConsultaDetalhada>(s => s
                .Index(INDEX)
                .Type("prodsense")
                .SearchType(Elasticsearch.Net.SearchType.Count)
                .Query(filterQuery2)
                .Query(q => q.QueryString(d => d.DefaultField("descricao_detalhada_produto").Query(paramatro.ToLower().Replace(" ", " AND "))))
                .Aggregations(a => a
                    .Terms("ncm", term => term
                        .Field("ncm")
                        .Aggregations(agg => agg
                            .ValueCount("countReg", st => st
                                .Field("ncm")
                            )
                            .Sum("vl_ift", st => st
                                .Field("CIF")
                            )
                        )
                    )
                )
            );

            var listBuckets = (Bucket)result.Aggregations["ncm"];

            List<ResumoConsultaDetalhada> listResumo = new List<ResumoConsultaDetalhada>();

            foreach (KeyItem listKeyItem in listBuckets.Items)
            {
                ResumoConsultaDetalhada resumo = new ResumoConsultaDetalhada();
                resumo.countReg = (long)((ValueMetric)listKeyItem.Aggregations["countReg"]).Value;
                resumo.vl_ift = (float)((ValueMetric)listKeyItem.Aggregations["vl_ift"]).Value;
                listResumo.Add(resumo);
            }

            return listResumo;
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to world of elasticSearch!");

            var node = new Uri("http://localhost:9200");

            var settings = new ConnectionSettings(
                node,
                defaultIndex: "uauto");

            var client = new ElasticClient(settings);

            //load data to ES
            //using (Context ctx = new Context())
            //{

            //    foreach (UAutoContractJournalItem item in ctx.UAutoContractJournalItem)
            //    {
            //        client.Index(item);
            //    }

            //}

            #region Use Object Initializer Syntax

            QueryContainer query = new MatchQuery
            {
                Field = "insuredLastName",
                Query = "Тарелкин"
            };

            var searchRequest = new SearchRequest
            {
                From = 0,
                Size = 10,
                Query = query

            };

            var searchResult = client.Search<UAutoContractJournalItem>(searchRequest);

            Console.WriteLine(Encoding.UTF8.GetString(searchResult.RequestInformation.Request));

            Console.WriteLine("{0} was found (OIS)", searchResult.Total);

            #endregion

            #region Lambda version

            searchResult = client.Search<UAutoContractJournalItem>(s => s
                .From(0)
                .Size(10)
                .Query(q => q.Term(p => p.InsuredLastName, "Тарелкин"))
                );

            searchResult = client.Search<UAutoContractJournalItem>(s => s
                .From(0)
                .Size(10)
                .Query(q => q.Match(x => x.OnField(f => f.InsuredLastName).Query("Тарелкин")))
                );

            Console.WriteLine(Encoding.UTF8.GetString(searchResult.RequestInformation.Request));
            Console.WriteLine("{0} was found (Lambda)", searchResult.Total);

            #endregion

            Console.WriteLine("Work complete");
            Console.ReadKey();
        }
Exemplo n.º 22
0
        /** 
	     * An example of using `OnRequestCompleted()` for complex logging. Remember, if you would also like 
         * to capture the request and/or response bytes, you also need to set `.DisableDirectStreaming()`
         * to `true`
		*/
        [U]public async Task UsingOnRequestCompletedForLogging()
		{
		    var list = new List<string>();
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var settings = new ConnectionSettings(connectionPool, new InMemoryConnection())
                .DisableDirectStreaming()
				.OnRequestCompleted(response =>
				{
                    // log out the request
                    if (response.RequestBodyInBytes != null)
                    {
                        list.Add(
                            $"{response.HttpMethod} {response.Uri} \n" +
                            $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                    }
                    else
                    {
                        list.Add($"{response.HttpMethod} {response.Uri}");
                    }

                    // log out the response
                    if (response.ResponseBodyInBytes != null)
                    {
                        list.Add($"Status: {response.HttpStatusCode}\n" +
                                 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                    else
                    {
                        list.Add($"Status: {response.HttpStatusCode}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                });

			var client = new ElasticClient(settings);

            var syncResponse = client.Search<object>(s => s
                .Scroll("2m")
                .Sort(ss => ss
                    .Ascending(SortSpecialField.DocumentIndexOrder)
                )
            );

            list.Count.Should().Be(2);

            var asyncResponse = await client.SearchAsync<object>(s => s
                .Scroll("2m")
                .Sort(ss => ss
                    .Ascending(SortSpecialField.DocumentIndexOrder)
                )
            );

            list.Count.Should().Be(4);
            list.ShouldAllBeEquivalentTo(new []
            {
                "POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
                "Status: 200\n------------------------------\n",
                "POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
                "Status: 200\n------------------------------\n"
            });
        }
Exemplo n.º 23
0
        private static void QueryNestedBlogs(ElasticClient elastic)
        {
            var nestedRes = elastic.Search<BlogPost>(s => s
                .Query(q => q
                    .Nested(n => n
                        .Path(b => b.Author)
                        .Query(nq =>
                            nq.Match(m1 => m1.Field(f1 => f1.Author.FirstName).Query("John")) &&
                            nq.Match(m2 => m2.Field(f2 => f2.Author.LastName).Query("Doe")))
                    )));

            Console.WriteLine(nestedRes.ApiCall.Success);
            Console.WriteLine(nestedRes.Hits.Count());

            foreach (var hit in nestedRes.Hits)
            {
                Console.WriteLine(hit.Source);
            }
        }
Exemplo n.º 24
0
        public static List<AgregationsPorBucketQtde> ConsultaElasticSearchDocCompany(string parametro)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            var filterQuery = Query<ES_DOCUMENTS_POCO>.MultiMatch(mm => mm
                                                                        .Query(parametro.ToLower())
                                                                        .OnFields(
                                                                            f => f.tx_descricaoMercadoria,
                                                                            f => f.txmercadoria)
                                                                                );
            var result = client.Search<AgregationsPorBucketQtde>(s => s
                                                                 .Index(INDEX)
                                                                .SearchType(Elasticsearch.Net.SearchType.Count)
                                                                .AllTypes()
                                                                .Query(filterQuery)
                                                                .Size(50)
                                                                 .Aggregations(a => a
                                                                                .Terms(DI, terDI => terDI
                                                                                        .Field("tx_cnpj")
                                                                                )
                                                                                .Terms(CE, terCE => terCE
                                                                                        .Field("cdconsignatario")
                                                                                )
                                                                              )
                                                                 );

            List<AgregationsPorBucketQtde> listResultados = new List<AgregationsPorBucketQtde>();

            //DI Values
            var listBucketsDI = (Bucket)result.Aggregations[DI];

            foreach (KeyItem listKeyItem in listBucketsDI.Items)
            {
                AgregationsPorBucketQtde resumoBusca = new AgregationsPorBucketQtde();
                //incluir aqui a busca pelo nome da empresa
                //resumoBusca.name = DIDAO.ConsultaEmpresaDIPorCnpj(listKeyItem.Key) + "/" + listKeyItem.Key;
                resumoBusca.qtde = listKeyItem.DocCount;

                listResultados.Add(resumoBusca);
            }
            //CE Values
            var listBucketsCE = (Bucket)result.Aggregations[CE];

            foreach (KeyItem listKeyItem in listBucketsCE.Items)
            {
                AgregationsPorBucketQtde resumoBusca = new AgregationsPorBucketQtde();
                //resumoBusca.name = CEDAO.ConsultaEmpresaCEPorCnpj(listKeyItem.Key) + "/" + listKeyItem.Key;
                resumoBusca.qtde = listKeyItem.DocCount;

                listResultados.Add(resumoBusca);
            }
            return listResultados;
        }
Exemplo n.º 25
0
        public static List<long> ConsultaElasticSearchListDocumentos(string paramatro, string docType)
        {
            var client = new ElasticClient(new ConnectionSettings(new Uri(URI_ES)));
            List<long> listResult = new List<long>();
            var pk = "";
            var searchField = "";
            if (docType.Equals(DI))
            {
                pk = "pk_di";
                searchField = "tx_descricaoMercadoria";
            }
            else if (docType.Equals(CE))
            {
                pk = "pk_mercadoria";
                searchField = "txmercadoria";
            }

            var result = client.Search<dynamic>(c => c
                                    .Index(INDEX)
                                    .Type(docType.ToLower())
                                    .Size(10000)
                                    .Fields(pk)
                                    .Query(q => q
                                        .Match(m => m
                                            .OnField(searchField)
                                            .Query(paramatro)
                                        )
                                    )
                                );
            foreach (var doc in result.FieldSelections)
                listResult.Add(doc.FieldValues<long>(pk));
            return listResult;
        }
Exemplo n.º 26
0
        public static ISearchResponse <object> _searchData(DataSet ds, string queryString, int page, int pageSize, string sort = null,
                                                           bool excludeBigProperties = true, bool withHighlighting = false, bool exactNumOfResults = false)
        {
            SortDescriptor <object> sortD = new SortDescriptor <object>();

            if (sort == "0")
            {
                sort = null;
            }

            if (!string.IsNullOrEmpty(sort))
            {
                if (sort.EndsWith(DataSearchResult.OrderDesc) || sort.ToLower().EndsWith(DataSearchResult.OrderDescUrl))
                {
                    sort  = sort.Replace(DataSearchResult.OrderDesc, "").Replace(DataSearchResult.OrderDescUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Descending);
                }
                else
                {
                    sort  = sort.Replace(DataSearchResult.OrderAsc, "").Replace(DataSearchResult.OrderAscUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Ascending);
                }
            }


            Nest.ElasticClient client = Lib.ES.Manager.GetESClient(ds.DatasetId, idxType: ES.Manager.IndexType.DataSource);

            QueryContainer qc = GetSimpleQuery(ds, queryString);

            //QueryContainer qc = null;
            //if (queryString == null)
            //    qc = new QueryContainerDescriptor<object>().MatchNone();
            //else if (string.IsNullOrEmpty(queryString))
            //    qc = new QueryContainerDescriptor<object>().MatchAll();
            //else
            //{
            //    qc = new QueryContainerDescriptor<object>()
            //        .QueryString(qs => qs
            //            .Query(queryString)
            //            .DefaultOperator(Operator.And)
            //        );
            //}

            page = page - 1;
            if (page < 0)
            {
                page = 0;
            }
            if (page * pageSize > Lib.Data.Smlouva.Search.MaxResultWindow)
            {
                page = (Lib.Data.Smlouva.Search.MaxResultWindow / pageSize) - 1;
            }

            //exclude big properties from result
            var maps = excludeBigProperties ? ds.GetMappingList("DocumentPlainText").ToArray() : new string[] { };



            var res = client
                      .Search <object>(s => s
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       .Highlight(h => Lib.Searching.Tools.GetHighlight <Object>(withHighlighting))
                                       .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                       );

            //fix Highlighting for large texts
            if (withHighlighting &&
                res.Shards != null &&
                res.Shards.Failed > 0)    //if some error, do it again without highlighting
            {
                res = client
                      .Search <object>(s => s
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       .Highlight(h => Lib.Searching.Tools.GetHighlight <Object>(false))
                                       .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                       );
            }

            Audit.Add(Audit.Operations.Search, "", "", "Dataset." + ds.DatasetId, res.IsValid ? "valid" : "invalid", queryString, null);

            return(res);
        }
Exemplo n.º 27
0
        public static List<PRODUTOS_SENSIVEIS_POCO> ConsultaProdutosSensiveisElasticSearch(string paramatro, string ncm, int startIndex, int blockSize)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            var filterQuery2 = Query<PRODUTOS_SENSIVEIS_POCO>.Terms("ncm", ncm.ToLower());

            var result = client.Search<PRODUTOS_SENSIVEIS_POCO>(s => s
                .Index(INDEX)
                .Type("prodsense")
                .Query(filterQuery2)
                .Query(q => q.QueryString(d => d.DefaultField("descricao_detalhada_produto").Query(paramatro.ToLower().Replace(" ", " AND "))))
                .Size(blockSize)
                .Skip((startIndex - 1) * blockSize)
            );

            return (List<PRODUTOS_SENSIVEIS_POCO>)result.Documents;
        }
Exemplo n.º 28
0
        public static List<ResumoConsulta> ConsultaNCMElasticSearch(string paramatro)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            var result = client.Search<ResumoConsulta>(s => s
                .Index(INDEX)
                .Type("prodsense")
                .SearchType(Elasticsearch.Net.SearchType.Count)
                //.Query(filterQuery)
                .Query(q => q.QueryString(d => d.DefaultField("descricao_detalhada_produto").Query(paramatro.ToLower().Replace(" "," AND "))))
                .Aggregations(a => a
                    .Terms("ncm", st => st
                        .Field("ncm")
                        .Size(99999999)
                        .Aggregations(aa => aa
                            .ValueCount("qtde", m => m
                                .Field("ncm")
                            )
                            .Sum("CIFTot", m => m
                                .Field("CIF")
                            )
                            .Average("vl_ift", m => m
                                .Field("vl_ift")
                            )
                        )
                    )
                     .Terms("desc", st => st
                        .Field("ncmDescricao")
                        .Size(99999999)
                    )
                )
            );

            var listBuckets = (Bucket)result.Aggregations["ncm"];

            Bucket listBucketsDesc = null;

            listBucketsDesc = (Bucket)result.Aggregations["desc"];

            List<ResumoConsulta> listResultados = new List<ResumoConsulta>();

            int count = 0;
            foreach (KeyItem listKeyItem in listBuckets.Items)
            {
                ResumoConsulta resumoBusca = new ResumoConsulta();
                resumoBusca.ncm = listKeyItem.Key;
                resumoBusca.countReg = (long)((ValueMetric)listKeyItem.Aggregations["qtde"]).Value;
                resumoBusca.CIFTot = (float)((ValueMetric)listKeyItem.Aggregations["CIFTot"]).Value;
                resumoBusca.vl_ift = (float)((ValueMetric)listKeyItem.Aggregations["vl_ift"]).Value;

                int countDesc = 0;
                foreach (KeyItem listKeyItemDesc in listBucketsDesc.Items)
                {
                    if (count == countDesc)
                    {
                        resumoBusca.tx_ncm_desc = listKeyItemDesc.Key;
                        break;
                    }
                    countDesc++;
                }

                listResultados.Add(resumoBusca);
                count++;
            }
            return listResultados;
        }
Exemplo n.º 29
0
        public static List<AgregationsPorBucketValor> ConsultaElasticSearchSumValor(string paramatro, string type_document, string campo, int qtde_registros, string index, string campo_sum, string url)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            //var filterQuery = Query<PRODUTOS_SENSIVEIS_POCO>.Terms("descricao_detalhada_produto", paramatro.ToLower().Replace(" ", " AND "));

            var result = client.Search<AgregationsPorBucketValor>(s => s
                .Index(index)
                .Type(type_document)
                .SearchType(Elasticsearch.Net.SearchType.Count)
                //.Query(filterQuery)
                .Query(q => q.QueryString(d => d.DefaultField("descricao_detalhada_produto").Query(paramatro.ToLower().Replace(" ", " AND "))))
                .Aggregations(a => a
                    .Terms("name", st => st
                        .Field(campo)
                        .Size(qtde_registros)
                        .Aggregations(aa => aa
                            .Sum("valor", m => m
                                .Field(campo_sum)
                            )
                        )
                    )
                     .Terms("desc", st => st
                        .Field("DESCRICAO")
                        .Size(qtde_registros)
                    )
                )
            );

            var listBuckets = (Bucket)result.Aggregations["name"];

            Bucket listBucketsDesc = null;

            if (campo.Equals("ncm"))
                listBucketsDesc = (Bucket)result.Aggregations["desc"];

            List<AgregationsPorBucketValor> listResultados = new List<AgregationsPorBucketValor>();

            int count = 0;
            foreach (KeyItem listKeyItem in listBuckets.Items)
            {
                AgregationsPorBucketValor resumoBusca = new AgregationsPorBucketValor();
                resumoBusca.name = listKeyItem.Key;
                resumoBusca.valor = (long)((ValueMetric)listKeyItem.Aggregations["valor"]).Value;
                resumoBusca.url = url;
                if (campo.Equals("ncm"))
                {

                    //This call will Load NCM´s list only once
                    //NcmDAO.loadStaticNCM();
                    //resumoBusca.desc = StaticNCM.getNCMDesc(resumoBusca.name);
                    int countDesc = 0;
                    foreach (KeyItem listKeyItemDesc in listBucketsDesc.Items)
                    {
                        if (count == countDesc)
                        {
                            resumoBusca.desc = listKeyItemDesc.Key;
                            break;
                        }
                        countDesc++;
                    }
                }
                listResultados.Add(resumoBusca);
                count++;
            }

            return listResultados;
        }
Exemplo n.º 30
0
        private static List<AgregationsPorBucketQtdexDate> searchQtdxDate(ElasticClient client, string filter_field, string parameter, string doc_type, string date_field)
        {
            List<AgregationsPorBucketQtdexDate> listResultados = new List<AgregationsPorBucketQtdexDate>();
            var filterQuery = Query<ES_DOCUMENTS_POCO>.Terms(filter_field, parameter.ToLower());

            var result = client.Search<AgregationsPorBucketQtdexDate>(s => s
                .Index(INDEX)
                .Type(doc_type)
                .SearchType(Elasticsearch.Net.SearchType.Count)
                .Query(filterQuery)
                .Aggregations(a => a
                    .DateHistogram("name", dh => dh
                        .Field(date_field)
                        .Interval("month")
                        .Format("yyyy-mm")
                    )
                )
            );

            var listBuckets = (Bucket)result.Aggregations["name"];
            foreach (HistogramItem listKeyItem in listBuckets.Items)
            {
                AgregationsPorBucketQtdexDate resumoBusca = new AgregationsPorBucketQtdexDate();
                resumoBusca.name = doc_type.ToUpper();
                resumoBusca.qtde = listKeyItem.DocCount;
                resumoBusca.eventdate = String.Format("{0:MM-yyyy}", listKeyItem.Date);

                listResultados.Add(resumoBusca);
            }
            return listResultados;
        }
Exemplo n.º 31
0
        public static ISearchResponse <object> _searchData(DataSet ds, string queryString, int page, int pageSize, string sort = null, bool excludeBigProperties = true)
        {
            SortDescriptor <object> sortD = new SortDescriptor <object>();

            if (sort == "0")
            {
                sort = null;
            }

            if (!string.IsNullOrEmpty(sort))
            {
                if (sort.EndsWith(DataSearchResult.OrderDesc) || sort.ToLower().EndsWith(DataSearchResult.OrderDescUrl))
                {
                    sort  = sort.Replace(DataSearchResult.OrderDesc, "").Replace(DataSearchResult.OrderDescUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Descending);
                }
                else
                {
                    sort  = sort.Replace(DataSearchResult.OrderAsc, "").Replace(DataSearchResult.OrderAscUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Ascending);
                }
            }


            Nest.ElasticClient client = Lib.ES.Manager.GetESClient(ds.DatasetId, idxType: ES.Manager.IndexType.DataSource);

            QueryContainer qc = GetSimpleQuery(ds, queryString);

            //QueryContainer qc = null;
            //if (queryString == null)
            //    qc = new QueryContainerDescriptor<object>().MatchNone();
            //else if (string.IsNullOrEmpty(queryString))
            //    qc = new QueryContainerDescriptor<object>().MatchAll();
            //else
            //{
            //    qc = new QueryContainerDescriptor<object>()
            //        .QueryString(qs => qs
            //            .Query(queryString)
            //            .DefaultOperator(Operator.And)
            //        );
            //}

            page = page - 1;
            if (page < 0)
            {
                page = 0;
            }
            if (page * pageSize > Lib.ES.SearchTools.MaxResultWindow)
            {
                page = (Lib.ES.SearchTools.MaxResultWindow / pageSize) - 1;
            }

            //exclude big properties from result
            var maps = excludeBigProperties ? ds.GetMappingList("DocumentPlainText").ToArray() : new string[] { };


            var res = client
                      .Search <object>(s => s
                                       .Type("data")
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       );

            return(res);
        }
Exemplo n.º 32
0
        public static PRODUTOS_SENSIVEIS_POCO ConsultaProdutoSensivelElasticSearch(int pk_produto_sensivel)
        {
            var node = new Uri(URI_ES);
            var settings = new ConnectionSettings(node);
            var client = new ElasticClient(settings);
            var filterQuery = Query<PRODUTOS_SENSIVEIS_POCO>.Terms("pk_ncmrf_15a", pk_produto_sensivel.ToString().Split(' '));

            var result = client.Search<PRODUTOS_SENSIVEIS_POCO>(s => s
                .Index(INDEX)
                .Type("prodsense")
                .Query(filterQuery)

            );

            return ((List<PRODUTOS_SENSIVEIS_POCO>)result.Documents).First();
        }
Exemplo n.º 33
-1
		[U] public void SearchDoesNotTakeDefaultIndexIntoAccount()
		{
			var node = new Uri("http://localhost:9200");
			var connectionPool = new SingleNodeConnectionPool(node);
			var connectionSettings = new ConnectionSettings(connectionPool, connection: new InMemoryConnection())
				.DefaultIndex("logstash-*")
				.DefaultFieldNameInferrer(p => p)
				.OnRequestCompleted(info =>
				{
					// info.Uri is /_search/ without the default index
					// my ES instance throws an error on the .kibana index (@timestamp field not mapped because I sort on @timestamp)
				});

			var client = new ElasticClient(connectionSettings);
			var response = client.Search<ESLogEvent>(s=>s);

			response.ApiCall.Uri.AbsolutePath.Should().Be("/logstash-%2A/eslogevent/_search");

			response = client.Search<ESLogEvent>(new SearchRequest<ESLogEvent>{ });
			response.ApiCall.Uri.AbsolutePath.Should().Be("/logstash-%2A/eslogevent/_search");

			response = client.Search<ESLogEvent>(new SearchRequest { });
			response.ApiCall.Uri.AbsolutePath.Should().Be("/_search");

		}
        // GET: Search
        public ActionResult Index(string r)
        {
            var uri = new Uri("https://IlwEAOgvDkuHk3yiB74RhwSs1YC0KCUu:@aniknaemm.east-us.azr.facetflow.io");
            var settings = new ConnectionSettings(uri).SetDefaultIndex("indexdem");
            var client = new ElasticClient(settings);

            var result = client.Search<Demotivator>(q => q
            .Query(f => f
               .QueryString(t => t.Query(r + "*").OnFields(u => u.Name)) || f
               .QueryString(t => t.Query(r + "*").OnFields(u => u.Str1)))

            );
            SearchViewModel model = new SearchViewModel();
            List<Demotivator> tr = new List<Demotivator>();

            foreach (var t in result.Hits)
            {

                var sleep = (Demotivator)t.Source;
                int temp = new int();

                if (sleep != null)
                {

                    tr.Add(sleep);
                }
                else
                {
                }

            }
            model.demotivators = tr;
            return View(model);
        }
Exemplo n.º 35
-1
        static void Main(string[] args)
        {
            elasticSettings = new ConnectionSettings(new Uri("http://127.0.0.1:9200"))
                .SetDefaultIndex("people");

            client = new ElasticClient(elasticSettings);

            ///// ** Basic search with QueryString
            var searchResults = client.Search<Person>(s => s.Query(q => q.
                                                    QueryString(x => x.Query("toto").
                                                                        OnField(of => of.LastName))));

            foreach (var result in searchResults.Documents)
            {
                Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            }

            ///// ** Basic search on Message
            //var searchResults = client.Search<Person>(s => s.Query(q => q.
            //    QueryString(x => x.Query("brother").
            //        OnField(of => of.Message).
            //        Operator((Operator.and)))));

            //foreach (var result in searchResults.Documents)
            //{
            //    Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            //}

            Console.ReadKey();
        }