0

hi i am trying to do a search where i can find specific files using elastic search. This is my java client:

<dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.5.3</version>
</dependency>

I prepared a query as follows:

private static Query getQueryByStatus() {
        BoolQuery boolQuery = BoolQuery.of(b -> b
            .should(qb -> qb.match(m -> m.field("fileType").query("WAIT_EXP")))
            .should(qb -> qb.match(m -> m.field("fileType").query("EXP")))
        );

        return boolQuery._toQuery();
    }
Query testQuery = getQueryByStatus();

                        response = client.search(s -> s
                                .index(fileIndex)
                                .from(filterDto.getPage() * filterDto.getSize())
                                .size(filterDto.getSize())
                                .query(q -> q
                                    .bool(b -> b
                                        .must(matchCompany)
                                        .must(matchByWords)
                                        .should(testQuery) 
                                    )
                                ),
                            SearchData.class
                        );

My expectation here is that it finds files with filetype exp or wait_exp, but elastic brings them as well as unrelated files. Unfortunately, I could not see any clear explanation on the subject in the client's document I used.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
Levent
  • 1

1 Answers1

0

Based on above code i can see you are doing a must match even when you want to get the search based on file names.

You can use match_phrase_prefix type of query if you want to bring in the file that are starting with "WAIT_EXP*" & "EXP*"

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html

  • im storing the search data as class and file type is inside the class here is the file: `private Date annunciationDate; private FileType fileType; private String personName` and my FileType is an enum that contains the types WAIT_EXP, EXP etc. – Levent Aug 01 '23 at 11:12
  • @levent i think then you should use match_phrase to find the files specific FileType with the names "WAIT_EXP" and "EXP" Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html – Parth Pancholi Aug 03 '23 at 09:48