Does Elasticsearch 2.4 support .gte function? - elasticsearch

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

Related

Edit SearchSourceBuilder in Elasticsearch Java API

I am given an existing SearchSourceBuilder that needs to have a boolean clauses added to it.
How do I add the clause without changing the existing query using the Elasticsearch API?
public void example(SearchSourceBuilder searchSourceBuilder) {
SearchQuery originalQuery = searchSourceBuilder.query();
//Edit the originalQuery to include a boolean must_not
SearchSourceBuilder.query(originalQuery);
}

Serialize query from Nest client ElasticSearch 6.4

Till ElasticSearch 6.0 we were able to serialize the search request (object of SearchRequest) to a string
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
ElasticClient.Serializer.Serialize(searchRequest, mStream);
string rawQueryText = Encoding.ASCII.GetString(mStream.ToArray());
}
Example is here too serialize query from Nest client elastic search 2.3
But in 6.4 version that has been removed and I am not able to locate exactly where is the documentation to serialize the query with 6.4 version
https://github.com/elastic/elasticsearch-net
Can some one help me here?
You can use the extension method in ElasticsearchSerializerExtensions in Elasticsearch.Net
using Elasticsearch.Net;
using Nest;
var client = new ElasticClient();
var searchRequest = new SearchRequest
{
Query = new MatchAllQuery()
};
var json = client.RequestResponseSerializer.SerializeToString(searchRequest);

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.

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

Resources