Elastic search duplicate aggregation section in a query with NEST - elasticsearch

I am creating a simple elastic request with Nest.
var searchRequest = new SearchRequest()
{
Aggregations = new AggregationDictionary()
{
}
};
When I serialize the query with elasticClient.RequestResponseSerializer.SerializeToString(searchRequest the query I am seeing like this :
{"aggs":{},"aggregations":{}}
Aggregation sections were created two times.
What should I do?
Is this a problem?
My nest version and elastic Search version are 7.6.2
Thanks.

Related

Elasticsearch simultaneously search by two documents

I have two different documents in the Elasticsearch - Decision and Nomination.
Right now I can only search all of the documents wit Decision type.
I use Spring Data Elasticsearch for this purpose:
PageRequest pageRequest = DecisionUtils.createPageRequest(pageNumber, pageSize);
MultiMatchQueryBuilder fuzzyMmQueryBuilder = multiMatchQuery(query, "name", "description").fuzziness("AUTO");
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder().should(fuzzyMmQueryBuilder);
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withIndices(ESDecision.INDEX_NAME).withTypes(ESDecision.TYPE).withPageable(pageRequest);
NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.withQuery(boolQueryBuilder).withPageable(pageRequest).build();
return elasticsearchTemplate.queryForPage(nativeSearchQuery, ESDecision.class);
Is it possible to update this code to search by Decision and Nomination simultaneously in order to get the search result from both of them ? If the answer is yes - please show an example how to implement this and also please show how to determine at search results who is Decision and who is Nomination ? Is there any classification field that can be added into search result entity for this purpose ?

QueryFilterBuilder elastic search 2.4

I hav ethis piece of code now as of elastic search 2.4 QueryFilterBuilder is deprecated and its showing error.
QueryFilterBuilder queryFilterBuilder = FilterBuilders.queryFilter(esQueryBuilder); i tried to replace it with QueryBuider but its not accepting.
if (StringUtils.isNotBlank(textQuery)) {
QueryBuilder esQueryBuilder = CatalogKeywordSearchHelper.getAppropriateESQueryForUserQuery(storeId, textQuery);
QueryFilterBuilder queryFilterBuilder = FilterBuilders.queryFilter(esQueryBuilder);
filterBuildersMap.put(IdxSchemaConstants.TEXT_QUERY, queryFilterBuilder);
}
Query filters have been deprecated in ES 2.0 and since then queries can now be used in a filter context, you can simply use a bool/filter query and put your query in it:
if (StringUtils.isNotBlank(textQuery)) {
QueryBuilder esQueryBuilder = CatalogKeywordSearchHelper.getAppropriateESQueryForUserQuery(storeId, textQuery);
# change this line
BoolQueryBuilder queryFilterBuilder = QueryBuilders.boolQuery()
.filter(esQueryBuilder);
filterBuildersMap.put(IdxSchemaConstants.TEXT_QUERY, queryFilterBuilder);
}

Elasticsearch - get source field data with java api

I'm using elastic search with jest (as java client).
I need some fields that is in nested document and since cannot get nested fields as pair, I need '_source' to get them.
Here is previous question to get them in ES query[ Link ], and It works well.
BUT cannot convert its query as jest code.
Below is my try.
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(
query
)
.fields( // need _source but no method.
"oid",
"_source.events.activityoid",
"_source.events.worktime");
Try using fetchSource() like this:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
.query(query)
.fetchSource(new String[] {
"oid",
"events.activityoid",
"events.worktime"
}, null);

NEST ElasticSearch, Query Result Is Empty

I'm trying to query an elastic search index using a match_all query.
Uri uri = new Uri("http://10.10.10.67:9200");
ConnectionSettings connection = new ConnectionSettings(uri);
connection.SetDefaultIndex("leases");
int port = connection.Port;
ElasticClient client = new ElasticClient(connection);
var feeQueryObject = client.Search<FeeQueryResult>(s => s
.Type("leases").MatchAll());
Using sense, I get results but not with NEST.
Not sure if I have to set up my mapped class exactly how the schema is in the ElasticSearch document? - I only have a few properties in my class, not all of them.
Any ideas as to why there are no results returned?
This was my fault.
The type was incorrect. Should be 'fee' not 'leases'.

Filtered Query in Elasticsearch Java API

I am little bit confused while creating Filtered query in Elasticsearch Java API.
SearchRequestBuilder class has setPostFilter method, javadoc of this method clearly says that filter will be applied after Query is executed.
However, there is no setFilter method Or some other method which will allow to apply filter before
query is executed. How do I create filtered Query(which basically applies filter before query is executed) here? Am I missing something?
FilteredQueryBuilder builder =
QueryBuilders.filteredQuery(QueryBuilders.termQuery("test",
"test"),FilterBuilders.termFilter("test","test"));
It will build the filtered query...To filteredQuery, first argument is query and second arguments is Filter.
Update: Filtered query is depreciated in elasticsearch 2.0+.refer
Hope it helps..!
QueryBuilders.filteredQuery is deprecated in API v. 2.0.0 and later.
Instead, filters and queries have "equal rights". FilterBuilders no longer exists and all filters are built using QueryBuilders.
To implement the query with filter only (in this case, geo filter), you would now do:
QueryBuilder query = QueryBuilders.geoDistanceQuery("location")
.point(center.getLatitude(), center.getLongitude())
.distance(radius, DistanceUnit.METERS);
// and then...
SearchResponse resp = client.prepareSearch(index).setQuery(query);
If you want to query by two terms, you would need to use boolQuery with must:
QueryBuilder query = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("user", "ben"))
.must(QueryBuilders.termQuery("rank", "mega"));
// and then...
SearchResponse resp = client.prepareSearch(index).setQuery(query);
In case you just want to execute filter without query, you can do like this:
FilteredQueryBuilder builder = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.termFilter("username","stackoverflow"));
Ref: Filtering without a query
It seems that by wrapping the query (a BoolQueryBuilder object) by giving it as an argument to boolQuery().filter(..) and then setting that in the setQuery() as you suggested- then this can be achieved. The "score" key in the response is always 0 (though documents are found)
Be careful here! If the score is 0, the score has been calculated. That means the query is still in query context and not in filter context. In filter context the score is set to 1.0 (without calculation) source
To create a query in filter context without calculation of the score (score = 1.0) do the following (Maybe there is still a better way?):
QueryBuilder qb = QueryBuilders.constantScoreQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("name", "blub)));
This returns the same results like:
GET /indexName/typeName/_search
{
"filter": {
"query": {
"bool": {
"must": [
{ "match": {
"name": "blub"
}}
]
}
}
}
}
Since FilteredQueryBuilder is deprecated in the recent versions, one can use the QueryBuilders.boolQuery() instead, with a must clause for the query and a filter clause for the filter.
import static org.elasticsearch.index.query.QueryBuilders.*;
QueryBuilder builder = boolQuery().must(termQuery("test", "test")).filter( boolQuery().must(termQuery("test", "test")));

Resources