0
votes

I'm having a profoundly hard time getting this elasticsearch query to cooperate. I'm currently trying to use a Bool Query to get results for both exact and analyzed/fulltext searches.

Seems I cannot, for what ever reason, use match filters inside a bool query:

{
   "bool":{
      "should":[
         "match":{"mainInfo.states": "Wisconsin"},
         "match":{"mainInfo.cities": "WI"}
      ]
   }
}

This throws a parser error, telling me that match is not a query type.

I need the ability to have fulltext searches inside of a bool query that also has term filters. So essentially I'd like to have a query somewhat like this:

{
   "bool":{
      "must":[
         "term":{"mainInfo.clientId":"123456"}
      ],
      "should":[
         "match":{"mainInfo.states": "Wisconsin"},
         "match":{"mainInfo.cities": "WI"}
      ]
   }
}

Where I can have 1 or 2 terms which must exists, and several fulltext searches where 2 of them should exist.

I'm also using a function_score query to return random, page-able results using a random_seed.

My problem is that I cannot get fulltext queries to run inside of my bool query. Seems that only term filters work. Also when I put the match queries out of the bool query, it seems that either the match query works, or the bool query works, but never both.

I cannot seem to form a query that will match several term queries, and several match queries. The term must exist, where as the match should exist(so at least matching one of the filters).

I'm by no means an elasticsearch expert, and the docs seem pretty vague when you're looking for information on more complicated queries.

If anyone could lend an example as how to query in such a manner, it would be greatly appreciated. However, please don't just link me to the docs for Bool Queries, or any other ElasticSearch docs. I assure you I've read them thoroughly, and I've tried a number of different ways to execute this query, but none seem to have the expected behavior.

I'm using ElasticSearch 1.3.2

1

1 Answers

2
votes

Seems I cannot, for what ever reason, use match filters inside a bool query:

The syntax of your query is not correct, if you have multiple clauses each clause should be wrapped in their own object:

{
  "query":{
    "custom_filters_score":{
      "query":{
        "nested":{
          "path":"mainInfo",
          "query":{
            "bool":{
              "should":[
                {
                  "match":{
                    "mainInfo.states":"WI"
                  }
                },
                {
                  "match":{
                    "mainInfo.cities":"Wisconsin"
                  }
                }
              ]
            }
          }
        }
      },
      "filters":[{
        "term":{
          "mainInfo.clientId":"123456"
        },
        "script":"yourscriptgoeshere"
      }]
    }
  }
}