I use spring data elastic search and have a this list in my elastic search.
{"appUserId": "id-test-app-user11", "apkId": 1, "event": "INSTALL", "date": "2020-06-01"}
...
{"appUserId": "id-test-app-user168", "apkId": 1, "event": "INSTALL", "date": "2020-12-06"}
I want to count by day the number of install of an apkId between a date range.
With this request, I can get all data bewteen my date range and an apkId provided in parameter
LocalDate today = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(convertToDateViaInstant(beginningDate))
.lte(convertToDateViaInstant(today)));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
return apkHistoryRepo.search(query);
But I don't know how to aggregate by date in order to have something like
{"2020-06-01": "500"}
...
{"2020-12-06": "10"}
Please how could I achieve this ?
Thanks in advance
You are looking for date histogram aggregation. Here is how you can use it in your query,
{
"query": {
"bool": {
"must": [
{
"term": {
"apkId": "1"
}
},
{
"term": {
"event": "INSTALL"
}
},
{
"range": {
"date": {
"gte": <start_date_here>,
"lte": <end_date_here>
}
}
}
]
}
},
"aggs": {
"per_day_count": {
"date_histogram": {
"field": "date",
"calendar_interval": "1d"
}
}
}
}
Final solution in Java
LocalDate today = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(beginningDate)
.lte(today));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
Iterable<ApkHistory> list = apkHistoryRepo.search(query);
AggregationBuilder aggregation = AggregationBuilders
.dateHistogram("nb_install_per_day")
.field("date")
.dateHistogramInterval(DateHistogramInterval.DAY);
SearchRequest searchRequest = new SearchRequest("apkhistory");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query).aggregation(aggregation);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedDateHistogram dateGroupBy = searchResponse.getAggregations().get("nb_install_per_day");
List<? extends Histogram.Bucket> bucketList = dateGroupBy.getBuckets();
for(Bucket b : bucketList) {
System.out.println(b.getKeyAsString() + " "+b.getDocCount());
}
System.out.println("test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
I have below code working fine in my java service.
Query searchQuery = new StringQuery(
"{\"bool\":{\"must\":[{\"match\":{\"id\":\"" + id + "\"}}]}}");
SearchHits<Instance> instanceSearchHits = elasticsearchOperations.search(searchQuery, Instance.class, IndexCoordinates.of("test"));
log.info("hits :: " + instanceSearchHits.getSearchHits().size());
Now, I want to save this query as a template in elastic and just pass params and search template from java service to elastic to execute the query.
Search Template added in Elastic
PUT _scripts/search-template-1
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{
"term": {
"id": "{{id}}"
}
}
]
}
}
},
"params": {
"id": "id to search"
}
}
}
Call this template
GET test/_search/template
{
"id": "search-template-1",
"params": {
"id": "f52c2c62-e921-4410-847f-25ea0f3eeb40"
}
}
But unfortunately not able to find API reference for the same to call this search template from JAVA (spring-data-elasticsearch)
As mentioned by val this can be used to call the search template query from java
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.STORED);
request.setScript("title_search");
Map<String, Object> params = new HashMap<>();
params.put("field", "title");
params.put("value", "elasticsearch");
params.put("size", 5);
request.setScriptParams(params);
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
SearchHits searchHits = searchResponse.getHits();
log.info("hits :: " + searchHits.getMaxScore());
searchHits.forEach(searchHit -> {
log.info("this is the response, " + searchHit.getSourceAsString());
});
This is currently not yet possible. There is an issue for this in Spring Data Elasticsearch.
I need some help in creating an AggregationDictionary from the following elasticsearch query
GET organisations/_search
{
"size": 0,
"aggs": {
"by_country": {
"nested": {
"path": "country"
},
"aggs": {
"by_country2": {
"filter": {
"bool": {
"must": [
{
"term": {
"country.isDisplayed": "true"
}
}
]
}
},
"aggs": {
"by_country3": {
"terms": {
"field": "country.displayName.keyword",
"size": 9999
}
}
}
}
}
}
}
}
I managed to write this horrible piece of code which I am pretty sure it is wrong, I am totally new to this.
AggregationDictionary aggs = new AggregationDictionary()
{
{
"countries_step1",
new NestedAggregation("countries_step1")
{
Path = "country",
Aggregations = new AggregationDictionary()
{
{
"countries_step2",
new FilterAggregation("countries_step2")
{
Filter = new BoolQuery
{
Must = new QueryContainer[] {
new NestedQuery
{
Query = new TermQuery
{
Field = "country.isDisplayed",
Value = true
}
}
}
},
Aggregations = new AggregationDictionary
{
{
"countries_step3",
new TermsAggregation("countries_step3")
{
Field = "country.displayName.keyword",
Size = 9999
}
}
}
}
}
}
}
}
};
Can someone tell me if I am in the correct direction? I am using Nest 6.6.0. Is there any tool that helps with these translations?
What you have so far is pretty solid, but when you try to execute this aggregation with the following call
var searchAsync = await client.SearchAsync<Document>(s => s.Size(0).Aggregations(aggs));
you will get this error
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "query malformed, empty clause found at [14:22]"
}
],
"type" : "illegal_argument_exception",
"reason" : "query malformed, empty clause found at [14:22]"
},
"status" : 400
}
Checking request which was sent to elasticsearch give us the answer why it happened
{
"aggs": {
"countries_step1": {
"aggs": {
"countries_step2": {
"aggs": {
"countries_step3": {
"terms": {
"field": "country.displayName.keyword",
"size": 9999
}
}
},
"filter": {}
}
},
"nested": {
"path": "country"
}
}
},
"size": 0
}
filter clause is empty, this is because you tried to used nested query but you didn't pass path parameter. We don't need nested query here (as shown in your example query), we can simplify the whole query to
var aggs = new AggregationDictionary()
{
{
"countries_step1",
new NestedAggregation("countries_step1")
{
Path = "country",
Aggregations = new AggregationDictionary()
{
{
"countries_step2",
new FilterAggregation("countries_step2")
{
Filter = new BoolQuery
{
Must = new QueryContainer[]
{
new TermQuery
{
Field = "country.isDisplayed",
Value = true
}
}
},
Aggregations = new AggregationDictionary
{
{
"countries_step3",
new TermsAggregation("countries_step3")
{
Field = "country.displayName.keyword",
Size = 9999
}
}
}
}
}
}
}
}
};
Now we have a valid request sent to elasticsearch.
There are a couple of things we can improve here:
1. Remove unnecessary bool query
Filter = new BoolQuery
{
Must = new QueryContainer[]
{
new TermQuery
{
Field = "country.isDisplayed",
Value = true
}
}
},
to
Filter =
new TermQuery
{
Field = "country.isDisplayed",
Value = true
},
2. Replace string field names
Usually, when doing calls from .Net there is some kind of POCO type which is helping us with writing strongly-typed requests to elasticsearch which helps us managing clean code and refactoring. With this, we can change field definition from
"country.displayName.keyword"
to
Infer.Field<Document>(f => f.Country.FirstOrDefault().DisplayName.Suffix("keyword"))
my types definition
public class Document
{
public int Id { get; set; }
[Nested]
public List<Country> Country { get; set; }
}
public class Country
{
public bool IsDisplayed { get; set; }
public string DisplayName { get; set; }
}
3. Consider using a fluent syntax
With NEST you can write queries in two ways: using object initializer syntax (which you did) or with help of fluent syntax. Have a look. Trying to write above query with the fluent syntax you will get something like
var searchResponse = await client.SearchAsync<Document>(s => s
.Size(0)
.Aggregations(a => a.Nested("by_country", n => n
.Path(p => p.Country)
.Aggregations(aa => aa
.Filter("by_country2", f => f
.Filter(q => q
.Term(t => t
.Field(field => field.Country.FirstOrDefault().IsDisplayed)
.Value(true)))
.Aggregations(aaa => aaa
.Terms("by_country3", t => t
.Field(field => field.Country.FirstOrDefault().DisplayName.Suffix("keyword"))
.Size(9999)
)))))));
which I find a little bit easier to follow and write, maybe it will be better for you as well.
As a final note, have a look into docs and check how you can debug your queries.
Hope that helps.
It's possible build a query with aggregation (elasticsearch), using java-query-dsl?
ElasticSearch provides a client lib that helps you to build searches. You can find more about it here.
Here's an example of how you can do it:
// build the client
HttpHost host = new HttpHost("localhost", 9200, "http");
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost[]{host}));
// build the search (set the conditions here)
BoolQueryBuilder boolQueryBuilder = boolQuery();
boolQueryBuilder.must(QueryBuilders.rangeQuery("age")
.from(25)
.to(40));
// build the aggregations (set the aggregations here)
TermsAggregationBuilder groupByGender = AggregationBuilders.terms("gender")
.field("gender")
.size(5);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);
sourceBuilder.aggregation(groupByGender);
// create and execute the search request
SearchRequest request = new SearchRequest()
.indices("customers")
.types("customer")
.allowPartialSearchResults(false)
.source(sourceBuilder)
.requestCache(true);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
which will produce something like:
GET customers/customer/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gt": 25,
"lt": 40
}
}
}
]
}
},
"aggs": {
"gender": {
"terms": {
"field": "gender",
"size": 5
}
}
}
}
I'm querying ElasticSearch using the Nest library for C#, to fetch graph data with multiple pivots. Each pivot is a nested TermsAggregation on a query, and everything works fine with one or two pivots. Once I get to three pivots, though, the SearchRequest object won't generate further aggregations.
The code to build the aggregations looks like this:
TermsAggregation topTermAgg = null;
TermsAggregation currentAgg = null;
foreach (var pivotName in activePivots)
{
newTermAgg = new TermsAggregation("pivot")
{
Field = pivot.ToString().ToLower()
};
if (topTermAgg == null)
{
topTermAgg = newTermAgg;
}
else
{
currentAgg.Aggregations = newTermAgg;
}
currentAgg = newTermAgg;
}
The SearchRequest itself is pretty straightforward:
var searchRequest = new SearchRequest(Indices.Index("a", "b", "c"))
{
Size = 0,
Aggregations = topTermAgg,
Query = query,
};
Unfortunately, the SearchRequest for 3 or more pivots, when converted to string, looks like this (via nestClient.Serializer.SerializeToString(searchRequest)):
{
"size": 0,
"query": {
"bool": <Fairly complex query, that works fine. It's the aggregation that has the problem.>
},
"aggs": {
"pivot": {
"terms": {
"field": "pivot1"
},
"aggs": {
"pivot": {
"terms": {
"field": "pivot2"
}
}
}
}
}
}
When I inspect the searchRequest object in the debugger, it quite definitely has 3 or more aggregations. What's going on here, and how can I get 3 or more nested terms aggregations to work properly?
I am using Nest version 5.01.
This must be related to the way in which you're building up the nested aggregations. Arbitrarily deep nested aggregations can be built with the client. Here's an example of a three deep nested aggregation
client.Search<Question>(s => s
.Aggregations(a => a
.Terms("top", ta => ta
.Field("top_field")
.Aggregations(aa => aa
.Terms("nested_1", nta => nta
.Field("nested_field_1")
.Aggregations(aaa => aaa
.Terms("nested_2", nnta => nnta
.Field("nested_field_3")
)
)
)
)
)
)
);
which serializes to
{
"aggs": {
"top": {
"terms": {
"field": "top_field"
},
"aggs": {
"nested_1": {
"terms": {
"field": "nested_field_1"
},
"aggs": {
"nested_2": {
"terms": {
"field": "nested_field_3"
}
}
}
}
}
}
}
}
You can also add values to AggregationDictionary directly
var request = new SearchRequest<Question>
{
Aggregations = new AggregationDictionary
{
{ "top", new TermsAggregation("top")
{
Field = "top_field",
Aggregations = new AggregationDictionary
{
{ "nested_1", new TermsAggregation("nested_1")
{
Field = "nested_field_1",
Aggregations = new AggregationDictionary
{
{ "nested_2", new TermsAggregation("nested_2")
{
Field = "nested_field_2"
}
}
}
}
}
}
}
}
}
};
client.Search<Question>(request);
is the same as the previous request. You can shorten this even further to
var request = new SearchRequest<Question>
{
Aggregations = new TermsAggregation("top")
{
Field = "top_field",
Aggregations = new TermsAggregation("nested_1")
{
Field = "nested_field_1",
Aggregations = new TermsAggregation("nested_2")
{
Field = "nested_field_2"
}
}
}
};
client.Search<Question>(request);
I got my code working by constructing the aggregation from the bottom-up, rather than from the top-down.
var terminalAggregation = <some aggregation. In my code, there's a lowest aggregation that's different from the rest. For the code I presented, you could just build the lowest pivot.>
TermsAggregation topTermAgg = null;
activePivots.Reverse();
foreach (var pivotName in activePivots)
{
newTermAgg = new TermsAggregation("pivot")
{
Field = pivot.ToString().ToLower(),
Aggregations = topTermAgg ?? terminalAggregation
};
topTermAgg = newTermAgg;
}
This looks like a bug in the Nest library; there are different classes like AggregationBase and BucketAggregationBase and AggregationDictionary that are all assignable to the "Aggregations" property, but it seems like there's some subtle flaw after the second assignment when you do this recursively.
The documentation is also not up-to-date: it claims that you can create an AggregationDictionary yourself, but since AggregationDictionary doesn't have a public Add() method, I really can't. Nor can I use the C#'s {}-after-insantiation syntax to populate its properties – again, because Add() is not public.
Trying to achieve following,
Retrieve articles that match given id and genre
Retrieve selected fields for matching records
I have tried this with Sense(chrome plugin),
POST /d3acampaign/article/_search
{
"fields": ["title","genre"] ,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"term": {
"id": "6"
}
},
{
"term": {
"genre": "metal"
}
}]
}
}
}
}
}
For C# code i am trying to build the query using following construct,
FilterContainer fc = null;
TermFilter title = new TermFilter()
{
Field = "id",
Value = "6",
};
TermFilter genre = new TermFilter()
{
Field = "genre",
Value = "metal",
};
fc = title & genre;
QueryContainer qc = new FilteredQuery() { Filter = fc };
var searchRequest = new SearchRequest
{
SearchType = Elasticsearch.Net.SearchType.QueryAndFetch,
Query = qc,
Indices = new IndexNameMarker[] {"journal"},
Types = new TypeNameMarker[] { "article" },
};
var r = client.SearchAsync<Article>(searchRequest);
var l = (List<Article>) r.Result.Documents;
I am able to run this query and get matching records, but i amnt sure how to specify selected fields to retrieve. Let me know what can be changed in C# code to specify necessary fields.
Thanks in advance.
Based on this answer you can modify your request object as follow:
var searchRequest = new SearchRequest
{
...
Fields = new List<PropertyPathMarker>
{
Property.Path<Article>(p => p.YourField)
}
};
or if you will decide to use source filtering:
var searchRequest = new SearchRequest
{
...
Source = new SourceFilter
{
Include = new []
{
Property.Path<Article>(p => p.YourField)
}
}
};
Hope it helps.