1

hi i am trying to query in ElasticSearch wherein I want to fetch data using multiMatchQuery to search a string and rangequery to filter data between the dates

Calendar compareDate = Calendar.getInstance();
compareDate.add(Calendar.DATE, 14);
SearchQuery query = new NativeSearchQueryBuilder().withIndices("project")
                .withPageable(new PageRequest(offset, limit))
                .withFilter(rangeQuery("startDate").from(new Date().getTime()).to(compareDate.getTime()))
                .withFilter(multiMatchQuery(string,
                        new String[] { "name","platform", "department", "url"}).build();

The date filter query fails and it gives me data of older dates. I tried using using booleanQueryBuilder still I got old dates. If i use rangeQuery filter without multiMatchQuery it gives me the correct data. but why this combined doesn't work? is there any work around using springdata?

my document will look like this

{
        "categories": [
            {
                "id": "ee625703-6103-6f94-b246-ff00003e0ef8",
                "name": "Networking"
            }
        ],
        "id": "PROJECT_69ee95dcb88547e388491328bbb6fdcc",
        "hostContentId": "69ee95dc-b885-47e3-8849-1328bbb6fdcc",
        "orgName": "Parinati Solutions",
        "title": "Parinati Solutions: Kansas City",
        "summary": "Parinati Solutions this is a test project name...",
        "categoryUrls": "networking",
        "startDate": 1512572400000,
        "endDate": 1512576000000,
        "friendlyDates": "Dec 6 @ 9:00 am - 10:00 am",
        "friendlyLocation": "Test local Foundation, Kansas City, MO",
        "providerSearchId": "9b89acbc-2d03-4526-860c-2a71f891be4b",
        "contentProvider": "sourcelink",
        "contentType": "PROJECT",
        "linkedinProfileUrl": null,
        "allowMemberContact": null,
        "firstName": null,
        "lastName": null,
        "expertise": null,
        "twitterHandle": null,
        "interests": null,
        "tagLine": null,
        "searchResultId": "9b89acbc-2d03-4526-860c-2a71f891be4b"
    }
Suyash
  • 331
  • 1
  • 4
  • 20

1 Answers1

2

You need to use withQuery() instead of withFilter() which is badly named and is essentially a post_filter.

You need to combine both of your queries inside a bool query:

withQuery(
    boolQuery()
       .must(multiMatchQuery(string, new String[] { "name","platform", "department", "url"}))
       .filter(rangeQuery("startDate").from(new Date().getTime()).to(compareDate.getTime()))
)
Val
  • 207,596
  • 13
  • 358
  • 360
  • SearchQuery query = new NativeSearchQueryBuilder().withIndices("project") .withPageable(new PageRequest(offset, limit)) .withQuery(QueryBuilders.boolQuery().must(multiMatchQuery(string, new String[] { "name","platform", "department", "url"})) .filter(rangeQuery("startDate").from(new Date().getTime()).to(compareDate.getTime().getTime()))).build(); – Suyash Dec 12 '17 at 15:01
  • Can you get the generated query and see how it looks like? If it doesn't return any document, can you show a document that you expect in the results and we'll fix what need be. – Val Dec 12 '17 at 15:09
  • my document will look something like this – Suyash Dec 12 '17 at 15:17
  • Update your question please, it'll be more legible – Val Dec 12 '17 at 15:19
  • hi Val can u plz tell me how to get the generated query also i will update the question and show you my document that i need – Suyash Dec 12 '17 at 15:21
  • See this thread on how to print the generated query: https://stackoverflow.com/a/43471315/4604579 – Val Dec 12 '17 at 15:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161040/discussion-between-suyash-and-val). – Suyash Dec 12 '17 at 15:33
  • You need to also add your mapping to the question. – Val Dec 27 '17 at 05:41
  • @Val could you help me out please https://stackoverflow.com/questions/65077447/merge-separate-queries-into-one-query – Catalina Nov 30 '20 at 16:33