For this query
Aggregation aggregation = Aggregation.newAggregation(
match(Criteria.where("userId").is(accountId)
.and("processingDay").gte(startMillis).lte(endMillis)),
sort(Sort.Direction.valueOf("ASC"), "x1"),
sort(Sort.Direction.valueOf("ASC"), "x2"),
Aggregation.group("A", "state")
.push(new BasicDBObject("x1", "$x1")
.append("x2", "$x2")
.append("A", "$A")
).as("XS"));
result is [{"A":null, "XS":{"A":NOT_NULL,...}}], how to fill A value where it is null?
The problem is group("A", "state") creates object like this:
"_id":{"A":"...", "state":"..."}
But, it expects (Output.class) the A field in the root document.
Just add the $first operator to add the 1st A value in the root document:
Aggregation aggregation = Aggregation.newAggregation(
match(Criteria.where("userId").is(accountId)
.and("processingDay").gte(startMillis).lte(endMillis)),
//sort(Sort.by(Direction.ASC, "x1", "x2")) // explicitly
sort(Sort.by("x1", "x2")), //Default is ASC
group("A", "state")
.first("A").as("A")
.push(new BasicDBObject("x1", "$x1")
.append("x2", "$x2")
.append("A", "$A")
).as("XS"));
Related
query = genericDAOImpl.getHibernateSession().createQuery("select t from ticket t where createdBy=:user and t.subject like concat("%", :summary, "%")
.setParameter("user", userId)
.setParameter("summary", inputBean.getSummary);
this is my query when I search for some values it returns the proper output but when I give null value for the search field it returns empty list actually it should return all the values when the search field is empty
You should just use a plain named placeholder for the predicate of the LIKE expression, and then bind the wildcard string from the Java side:
String sql = "select t from ticket t where createdBy = :user and t.subject like :summary";
query = genericDAOImpl.getHibernateSession().createQuery(sql);
query.setParameter("user", userId);
query.setParameter("summary", "%" + inputBean.getSummary + "%");
Using Query string OnFieldWithBoosts added different fields need to apply filter, string fields records are working fine.
For id field when I include .Add(id,2) it does not return ID based result.
When I use term then ID fields records working fine.
Now in the query section,
I have used OR condition, so when first condition satisfies, it does not check second one.
If I user AND condition, then it checks for both the condition matches.
But I need first query results and second query results concat into one result
CODE:
var result = client.Search<dynamic>(q => q
.Indices("test")
.Types("user")
.From(1)
.Size(10)
.MinScore(1.0)
.Fields("id", "createddate", "email", "modifieddate", "name", "companyname") // Result set Fields Fields
.Query(q1 =>
{
qq = (q1.ConstantScore(a => a.Filter(b => b.Term("id", searchKeyword))))
|| q1.QueryString(qs => qs.Query(searchKeyword).OnFieldsWithBoost(a => a.Add("notes",3).Add("email", 2).Add("name", 2)));
return qq;
})
);
How do I create a bucket aggregation based on a field value, and then run a query that gives me hits for each bucket (and not each document) with filters?
New in elastic search and I will appreciate any help!
I'm working my way through this currently. So far, I've figured out how to aggregate on a field, but haven't been able to apply filters yet. Please let me know if you've made progress as I see this was posted a while ago...
# connect to elastic search
elastic::connect('connection_string',
es_port = 9200)
# define aggregation
aggs <- list(
aggs = list(
field_name = list(
terms = list(
field = "field_name"
)
)
)
)
# search
Search(index = 'index_name',
body = aggs,
asdf = T)
I managed to apply filter for date as follows:
last_week <- query('{
"range" : {
"your_date_field" : {
"gte" : "mondays-date",
"lt" : "sundays-date"
}
}
}')
I used this as the main query. I think you can apply the aggregating to this with the %search% (last_week + agg) notation and it should work.
I have in db elements with following structure:
{
"id": 324214,
"modDate": "2014-10-01",
"otherInfo": {
..
..
}
}
Let's suppose that I have list of pairs [id, modDate]:
Map<String, String> idAndModDate
which contains f.e (324214, "2014-10-01"), (3254757, "2015-10-04")..
Now, I would like to use Java Api Elasticsearch QueryBuilder to build Query which in result give me list of all "ids" which are present in system but for who modDate is different as given.
Suppose that I have in database elements with following id/date pairs:
id, date
1, 2015-01-01
2, 2014-03-02
3, 2000-01-22
4, 2020-09-01
Now, I want to create query for
Map with following data:
Map<String, String> idDataPairs =[
(1, 2015-01-01)
(2, 2014-03-03)
(3, 2000-01-22)
(7, 2020-09-01)]
now I want create function like
List<String> ids = search(Map<String, String>) {
QueryBuilder.(sth).(sth) <--- thats what I'm asking about
}
which will return ids: 1, 3 because those ids exist in DB and dates from query are equal to dates in db respectively.
This is what you are looking for, more or less.
//build the test data in the map
Map<String, String> idDataPairs = new HashMap<String, String>();
idDataPairs.put("1", "2015-01-01");
idDataPairs.put("2", "2014-03-03");
idDataPairs.put("3", "2000-01-22");
idDataPairs.put("4", "2020-09-01");
//construct the query
BoolQueryBuilder should = QueryBuilders.boolQuery();
for(String id : idDataPairs.keySet()){
BoolQueryBuilder bool = QueryBuilders.boolQuery();
bool.must(QueryBuilders.termQuery("id", id));
bool.must(QueryBuilders.termQuery("modDate", idDataPairs.get(id)));
should.should(bool);
}
should.minimumNumberShouldMatch(1);
What i am doing is this:
For each of the Pairs, i am constructing a BoleanQuery called bool. This boolean query has two must conditions, that both the id and the date MUST match the document.
After constructing one bool Boolean Query, I add it to a parent BooleanQuery as well. This time, i say that the inner bool query should match, but its not required to. The final line says that at least one of these queries should match, if we want the document to match.
This structure is easier to understand, because must functions like AND and should functions like OR, but another way to do this is to use a TermsQuery, where we construct several TermsQuerys, and then add them to another parent BooleanQuery using should.
So, for the data
id, date
1, 2015-01-01
2, 2014-03-02
3, 2000-01-22
4, 2020-09-01
the above code will return the documents with ids 1,2,3
Example:
Parent doc
id: 1
field1: politics
field2: donkeys
Child doc
parent_id: 1
field1: prose
I would like to be able to search for the words 'politics donkey prose' (as an AND query, but not caring which fields any of the words match) and have it match the parent document. Is this possible? Or do I need to start rolling up the children as a big field within the parent (very undesirable because there can be many children)?
I am preferably looking for the solution in Java, but I will take it any way I can get it!
I think this might do it:
String parent_fields = "f1,f2";
String child_fields = "f3,f4";
BoolQueryBuilder allQueries = QueryBuilders.boolQuery();
for( String term : terms )
{
BoolQueryBuilder booly = QueryBuilders.boolQuery();
for( String field : parent_fields.split( "," ) )
{
booly.should( QueryBuilders.termQuery( field, term ) );
}
for( String field : child_fields.split( "," ) )
{
booly.should( QueryBuilders.topChildrenQuery( child_type, QueryBuilders.termQuery( field, term ) ) );
}
allBoolies.must( booly );
}