Exemple #1
0
        public async Task <IActionResult> GetApplicationName(string searchKey)
        {
            var result = await _client.SearchAsync <StringResponse>("mcrp-*", PostData.String($@"
            {{
                ""timeout"": ""1000ms"",
                ""terminate_after"": 100000,
                ""size"": 0,
                ""aggs"": {{
                    ""termsAgg"": {{
                        ""terms"": {{
                            ""order"": {{
                                ""_count"": ""desc""
                            }},
                            ""field"": ""fields.ApplicationName.keyword"",
                            ""include"": "".*{searchKey}.*""
                        }}
                    }}
                }},
                ""docvalue_fields"": [
		               {{ ""field"": ""@timestamp"", ""format"": ""date_time""}},
		               {{""field"": ""Commit.Timestamp"", ""format"": ""date_time""}},
		               {{ ""field"": ""Object_Attributes.Created_At"", ""format"": ""date_time""}},
		               {{ ""field"": ""Object_Attributes.Finished_At"",""format"": ""date_time""}},
		               {{ ""field"": ""fields.now"",""format"": ""date_time""}}
	                ],
	                ""query"": {{
                        ""bool"": {{
                            ""must"": [],
			                ""filter"": [
                                    {{
                                        ""range"": {{
                                        ""@timestamp"": {{""format"": ""strict_date_optional_time"",
							                        ""gte"": ""{System.DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd'T'HH:mm:ss.fff")}"",
							                        ""lte"": ""{System.DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fff")}"" }}
                                        }}
                                    }}
                            ],
			            ""should"": [],
                        ""must_not"": []
                    }}
                 }}
            }}"), new SearchRequestParameters()
            {
                IgnoreUnavailable = true, TotalHitsAsInteger = true
            });

            return(Ok(result.Body));
        }
        public async Task <Stream> GetLogs(JObject query, string indexPattern = BASIC_INDEX_PATTERN,
                                           List <string> tokensToIgnore       = null)
        {
            tokensToIgnore = tokensToIgnore ?? JsonPropertyEditor.BASIC_TOKENS_TO_IGNORE;
            try
            {
                var data = await client.SearchAsync <StringResponse>(indexPattern,
                                                                     JsonPropertyEditor.ExcludeGrayLogMeta(query, tokensToIgnore).ToString());

                var ms   = new MemoryStream();
                var json = JObject.Parse(data.Body);

                //elastic specification and graylog
                using (var r = new ChoJSONReader(json["hits"]["hits"].Select(x => x["_source"])))
                {
                    using (var w = new ChoCSVWriter(ms, new ChoCSVRecordConfiguration {
                        HasExcelSeparator = true
                    })
                                   .WithFirstLineHeader())
                    {
                        w.Write(r);
                    }
                }

                ms.Position = 0;
                return(ms);
            }
            catch (Exception e)
            {
                throw new Exception("ElasticSearch error!", e);
            }
        }
        public async Task <string> ResponseData(string search)
        {
            StringResponse searchResponse = await client.SearchAsync <StringResponse>(search);

            bool   successful   = searchResponse.Success;
            string responseJson = searchResponse.Body;

            return(successful ? responseJson : searchResponse.ApiCall.DebugInformation);
        }
        private async Task <List <DomainId> > SearchAsync(object query,
                                                          CancellationToken ct)
        {
            var result = await client.SearchAsync <DynamicResponse>(indexName, CreatePost(query), ctx : ct);

            if (!result.Success)
            {
                throw result.OriginalException;
            }

            var ids = new List <DomainId>();

            foreach (var item in result.Body.hits.hits)
            {
                if (item != null)
                {
                    ids.Add(DomainId.Create(item["_source"]["contentId"]));
                }
            }

            return(ids);
        }
Exemple #5
0
        public async Task <List <Guid>?> SearchAsync(string?queryText, IAppEntity app, SearchFilter?filter, SearchScope scope)
        {
            var serveField = GetServeField(scope);

            var query = new
            {
                query = new
                {
                    @bool = new
                    {
                        must = new List <object>
                        {
                            new
                            {
                                term = new Dictionary <string, object>
                                {
                                    ["appId.keyword"] = app.Id
                                }
                            },
                            new
                            {
                                term = new Dictionary <string, string>
                                {
                                    [serveField] = "true"
                                }
                            },
                            new
                            {
                                multi_match = new
                                {
                                    fields = new[]
                                    {
                                        "texts.*"
                                    },
                                    query = queryText
                                }
                            }
                        },
                        should = new List <object>()
                    }
                },
                _source = new[]
                {
                    "contentId"
                },
                size = 2000
            };

            if (filter?.SchemaIds.Count > 0)
            {
                var bySchema = new
                {
                    term = new Dictionary <string, object>
                    {
                        ["schemaId.keyword"] = filter.SchemaIds
                    }
                };

                if (filter.Must)
                {
                    [email protected](bySchema);
                }
                else
                {
                    [email protected](bySchema);
                }
            }

            var result = await client.SearchAsync <DynamicResponse>(IndexName, CreatePost(query));

            if (!result.Success)
            {
                throw result.OriginalException;
            }

            var ids = new List <Guid>();

            foreach (var item in result.Body.hits.hits)
            {
                if (item != null)
                {
                    ids.Add(Guid.Parse(item["_source"]["contentId"]));
                }
            }

            return(ids);
        }
        public async Task Elasticsearch_Span_Does_Not_Have_Http_Child_Span()
        {
            var payloadSender = new MockPayloadSender();

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender, configuration: new MockConfiguration(exitSpanMinDuration: "0", spanCompressionEnabled: "false"))))
                using (agent.Subscribe(new ElasticsearchDiagnosticsSubscriber(), new HttpDiagnosticsSubscriber()))
                {
                    var searchResponse = await agent.Tracer.CaptureTransaction("Call Client", ApiConstants.ActionExec,
                                                                               async() => await _client.SearchAsync <StringResponse>(PostData.Empty)
                                                                               );

                    searchResponse.Should().NotBeNull();
                    searchResponse.Success.Should().BeTrue();
                    searchResponse.AuditTrail.Should().NotBeEmpty();

                    var spans = payloadSender.SpansOnFirstTransaction;
                    spans.Should().NotBeEmpty().And.NotContain(s => s.Subtype == ApiConstants.SubtypeHttp);
                }
        }
Exemple #7
0
        async Task <ISearchResults> ISearch.Search(
            string[] securityIdentifiers,
            API.Common.Models.SearchRequest searchRequest)
        {
            var query = new
            {
                from = searchRequest.Paging.PageIndex * searchRequest.Paging.PageSize,
                size = searchRequest.Paging.PageSize,
                sort = new[]
                {
                    new { _score = new { order = "desc" } }
                },
                highlight = new
                {
                    pre_tags  = new[] { "<em>" },
                    post_tags = new[] { "</em>" },
                    fields    = new
                    {
                        content = new
                        {
                            number_of_fragments = 3,
                            boundary_max_scan   = 50,
                            type = "fvh"
                        }
                    }
                },
                aggs = new
                {
                    facet_strings = new
                    {
                        nested = new { path = "facets" },
                        aggs   = new
                        {
                            facet_name = new
                            {
                                terms = new { field = "facets.name" },
                                aggs  = new
                                {
                                    facet_value = new { terms = new { field = "facets.value", size = 10 } }
                                }
                            }
                        }
                    }
                },
                _source = new[] {
                    "uniqueKey",
                    "indexed",
                    "organizationKey",
                    "folderKey",
                    "fileKey",
                    "type",
                    "name",
                    "extension",
                    "mimeType",
                    "created",
                    "modified",
                    "mimeType",
                    "fields",
                    "metadata",
                    "facets"
                },
                query = new
                {
                    @bool = new
                    {
                        should = UserQuery(searchRequest),
                        filter = OrganizationAndFolderFilter(searchRequest.OrganizationIdentifier, searchRequest.FolderIdentifier)
                                 .Append(SecurityFilters(securityIdentifiers))
                                 .Append(NestedFacetFilters(searchRequest)),
                        minimum_should_match = 1
                    }
                }
            };


            var queryJSON      = JsonConvert.SerializeObject(query);
            var searchResponse = await lowLevelClient.SearchAsync <StringResponse>(IndexName, PostData.String(queryJSON));

            var deserialized = JsonConvert.DeserializeObject <SearchResposne>(searchResponse.Body);

            var searchResults = new SearchResults
            {
                Rows = deserialized.Hits.Hits.Select(d => new SearchResult
                {
                    FileIdentifier = new FileIdentifier
                    {
                        OrganizationKey = d._Source.OrganizationKey,
                        FolderKey       = d._Source.FolderKey,
                        FileKey         = d._Source.FileKey
                    },

                    Name      = d._Source.Name,
                    Extension = d._Source.Extension,
                    MimeType  = d._Source.MimeType,

                    Metadata = d._Source.Metadata,

                    Created    = d._Source.Created,
                    Modified   = d._Source.Modified,
                    Highlights = d.Highlight?.SelectMany(h => h.Value).ToArray(),

                    Fields = d._Source.Fields.Where(f => !String.IsNullOrWhiteSpace(f.Value)).ToDictionary(f => f.Key, f => f.Value)
                }),

                TotalMatches = deserialized.Hits.Total,
                DebugQuery   = queryJSON
            };

            var facetModels = new List <FacetModel>();

            foreach (var facet in deserialized.Aggregations.Facet_Strings.Facet_Name.Buckets)
            {
                var model = new FacetModel
                {
                    Name = facet.Key.ToString()
                };

                var values = new List <FacetValue>();
                model.Values = values;

                foreach (var facetValue in facet.Facet_Value.Buckets)
                {
                    values.Add(new FacetValue
                    {
                        Value = facetValue.Key.ToString(),
                        Count = facetValue.Doc_Count
                    });
                }

                if (model.Values.Any() && facet.Facet_Value.Sum_Other_Doc_Count == 0)
                {
                    if (model.Values.Count() == 1 &&
                        model.Values.First().Count == deserialized.Hits.Total)
                    {
                        // this is a single facet describing all
                        // results. it doesn't further differentiate the resultset.

                        // skip it
                    }
                    else
                    {
                        facetModels.Add(model);
                    }
                }
            }
            searchResults.Facets = facetModels;

            return(searchResults);
        }