vkrishna
(VAMSI)
September 26, 2024, 8:59am
1
Hi Team,
Need help in writing JAVA API query.
These are my elastic documents.
{
"number":1,
"effective_date":"2024-10-01T00:00:00.000Z"
}
{
"number":2,
"effective_date":"2024-10-02T00:00:00.000Z"
}
{
"number":3,
"effective_date":"2024-10-03T00:00:00.000Z"
}
{
"number":4,
"effective_date":"2024-10-04T00:00:00.000Z"
}
Now I have 2 conditions
List numbers = List.of(2,3,4);
Search with numbers list.
Apply DateRange gte on top of it "2024-10-03T00:00:00.000Z"
With this criteria
I should get 3rd and 4th documents in result.
Result size should be 2.
Thanks
dadoonet
(David Pilato)
September 26, 2024, 2:17pm
2
In your example, with this list of 2, 3, 4, only the daterange really matters.
I'd suggest a use-case with 2, 3 as values from the list and the same date range, which should give only the doc 3.
Something like:
void boolQuery() throws IOException {
client.index(ir -> ir.index(indexName).id("1").withJson(new StringReader("""
{
"number":1,
"effective_date":"2024-10-01T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("2").withJson(new StringReader("""
{
"number":2,
"effective_date":"2024-10-02T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("3").withJson(new StringReader("""
{
"number":3,
"effective_date":"2024-10-03T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("4").withJson(new StringReader("""
{
"number":4,
"effective_date":"2024-10-04T00:00:00.000Z"
}""")));
client.indices().refresh(rr -> rr.index(indexName));
SearchResponse<Void> response = client.search(sr -> sr
.index(indexName)
.query(q -> q.bool(bq -> bq
.filter(fq -> fq.terms(tq -> tq.field("number")
.terms(t -> t.value(List.of(
FieldValue.of("2"),
FieldValue.of("3"))))))
.filter(fq -> fq
.range(rq -> rq.date(drq -> drq
.field("effective_date")
.gte("2024-10-03T00:00:00.000Z"))))
))
, Void.class);
assertNotNull(response.hits().total());
assertEquals(1, response.hits().total().value());
assertEquals("3", response.hits().hits().get(0).id());
}
I documented it here:
@Test
void boolQuery() throws IOException {
client.index(ir -> ir.index(indexName).id("1").withJson(new StringReader("""
{
"number":1,
"effective_date":"2024-10-01T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("2").withJson(new StringReader("""
{
"number":2,
"effective_date":"2024-10-02T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("3").withJson(new StringReader("""
{
"number":3,
"effective_date":"2024-10-03T00:00:00.000Z"
}""")));
client.index(ir -> ir.index(indexName).id("4").withJson(new StringReader("""
{
"number":4,
This file has been truncated. show original
vkrishna
(VAMSI)
September 26, 2024, 4:01pm
3
Hi there,
I want pass list to the query.
looking for that query
dadoonet
(David Pilato)
September 26, 2024, 4:51pm
4
List.of
seems to be a List to me. What do you mean?
vkrishna
(VAMSI)
September 26, 2024, 5:41pm
5
vkrishna:
numbers
I want to use numbers in the query. I dont want to expand it.
dadoonet
(David Pilato)
September 26, 2024, 5:57pm
6
You mean that you don't want to convert a List of numbers to a List of FieldValue
?
vkrishna
(VAMSI)
September 26, 2024, 6:02pm
7
Yes. I dont want to expand my list.
vkrishna
(VAMSI)
September 26, 2024, 6:38pm
8
Convert is okay, but I dont want to expand my list. I have a big list.
dadoonet
(David Pilato)
September 26, 2024, 6:53pm
9
I don't understand. Anyway, I don't have any other idea.
vkrishna
(VAMSI)
September 26, 2024, 7:01pm
10
List.of(FieldValue.of("2"), FieldValue.of("3"))
I have a list of 100 numbers.
I cant expand like this for 100 numbers.
Can I pass it as list?
dadoonet
(David Pilato)
September 26, 2024, 7:30pm
11
I guess you can easily write a method which does that