QueryFilterBuilder elastic search 2.4 - elasticsearch

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);
}

Related

Elastic search duplicate aggregation section in a query with NEST

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.

How to search Exact string in elasticsearch DB

Search string in elastic search DB. Can any one please help to resolve this issue.
I am facing issue when I try using below snippet,
BoolQueryBuilder qb = QueryBuilders.boolQuery();
QueryBuilder qbm = QueryBuilders.matchQuery("Coloumname", "textValue");
Thanks.

How to create a search with or clause using Elasticsearch

I want to create a query with Elasticsearch Java API but I don't know how to create an OR clause? What I want to query is;
SELECT *
FROM USERS
WHERE (user.name = "admin") AND (user.message LIKE "test*") AND (user.age = "30" OR user.status = "major")
I have created a query like below but I don't know how to create an OR clause like sql query;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.must(QueryBuilders.matchQuery("message", "test*"));
boolQueryBuilder.must(QueryBuilders.matchQuery("age", "30"));
boolQueryBuilder.must(QueryBuilders.matchQuery("status","major"));
You simply need to capture the OR condition inside another bool/should query
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("message", "test*"));
BoolQueryBuilder orQuery = QueryBuilders.boolQuery();
orQuery.should(QueryBuilders.matchQuery("age", "30"));
orQuery.should(QueryBuilders.matchQuery("status","major"));
orQuery.minimumNumberShouldMatch(1);
boolQueryBuilder.must(orQuery);
PS: not sure why you have a mustNot for your second constraint.
The "should" keyword in bool queries works like "OR", but in Elasticsearch those queries are not very sharp as everything is scored.
Have a look at:
https://www.elastic.co/guide/en/elasticsearch/guide/current/bool-query.html#bool-query
Also there are very good examples in the book Elasticsearch in Action.

Does Elasticsearch 2.4 support .gte function?

Alternative for this as i am not getting for gte() as i am migrating elastic search from 1.4 to 2.4?
Double price;
NotFilterBuilder excMaxPriceFilter = FilterBuilders.notFilter(FilterBuilders.rangeFilter("exclude_max_price").gte(price));
You can rewrite your code to this:
BoolQueryBuilder excMaxPriceFilter = QueryBuilders.boolQuery()
.mustNot(QueryBuilders.rangeQuery("exclude_max_price").gte(price));

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