0
votes

I'm having trouble creating a query which should search for any documents with a certain search term in the fields title and text, and should match a state field which could be zero or more values where atleast one must match.

Given the following query:

  "bool" : {
    "must" : {
      "multi_match" : {
        "query" : "test",
        "fields" : [ "title", "text" ]
      }
    },
    "should" : {
      "terms" : {
        "state" : [ "NEW" ]
      }
    },
    "minimum_should_match" : "1"
  }

Should not the following data be returned as a result?

{
    "_shards": {
        "failed": 0,
        "successful": 5,
        "total": 5
    },
    "hits": {
        "hits": [
            {
                "_id": "JXnEkYFDQp2feATMzp2LTA",
                "_index": "tips",
                "_score": 1.0,
                "_source": {
                    "state": "NEW",
                    "text": "This is a test",
                    "title": "Test"
                },
                "_type": "tip"
            }
        ],
        "max_score": 1.0,
        "total": 1
    },
    "timed_out": false,
    "took": 1
}

In my test this is not the case. What am i doing wrong?

the following is the java code producing the outputted query.

    SearchRequestBuilder builder = client.prepareSearch("tips").setTypes("tip");
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

    if(searchTermIsNotEmpty(searchTerm)){
        MultiMatchQueryBuilder qb = QueryBuilders.multiMatchQuery(
                searchTerm,
                "title", "text"
        );
        boolQuery.must(qb);
    }

    if(filters.size() > 0){
        boolQuery.should(QueryBuilders.termsQuery("state",filters));
        boolQuery.minimumNumberShouldMatch(1);
    }

    if(boolQuery.hasClauses()){
        builder.setQuery(boolQuery);
    }

    logger.info(boolQuery.toString());


    SearchResponse result = builder.execute().actionGet();

    return result.toString();

Any help on this is greatly appreciated!

1

1 Answers

0
votes

Seems i found the issue, for some reason i was unable to fetch when using the filter enum in it's original form. I had to convert the enum to string and lowercase it.

I then added the following query

boolQuery.must(QueryBuilders.termsQuery("state", getLowerCaseEnumCollection(filters)).minimumMatch(1));

I'm new to elasticsearch, so i don't know if this is a bug, or a feature. Im just glad i figured it out.