Can't get Elasticsearch filter to work

97 Views Asked by At

using Elasticsearch 2 with Rails 4, using elasticsearch-model gem

Everything is fine and even geo-point distance is working. However, I can't work out for the life of me how to make a simple boolean filter work. I have a simple boolean 'exclude_from_search_results' that (when true) should cause the record to be filtered from the results.

Here's my query in rails controller (without the filter):

    @response = Firm.search(
    query: {
      bool: {
        should: [
          { multi_match: {
              query: params[:search],
              fields: ['name^10', 'address_1', 'address_2', 'address_3', 'address_4', 'address_5', 'address_6'],
              operator: 'or'
            }
          }
        ]
      }
    },
    aggs: {types: {terms: {field: 'firm_type'}}}
  )

I've added this both within the bool or query section, or outside it, but I either get NO documents, or all documents. (9000 should match)

Example:

  @response = Firm.search(
    query: {
      bool: {
        should: [
          { multi_match: {
              query: params[:search],
              fields: ['name^10', 'address_1', 'address_2', 'address_3', 'address_4', 'address_5', 'address_6'],
              operator: 'or'
            }
          }
        ],
            filter: {
            term: {"exclude_from_search_results": "false"}
        }
      }
    },
    aggs: {types: {terms: {field: 'firm_type'}}}
  )

I've also tried putting the filter clause in different places but either get error or no results. What am I doing wrong?? Probably missing something simple...

Here's my mapping:

    "mappings" : {
  "firm" : {
    "dynamic" : "false",
    "properties" : {
      "address_1" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "address_2" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "address_3" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "address_4" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "address_5" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "address_6" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      },
      "exlude_from_search_results" : {
        "type" : "boolean"
      },
      "firm_type" : {
        "type" : "string",
        "index" : "not_analyzed"
      },
      "location" : {
        "type" : "geo_point"
      },
      "name" : {
        "type" : "string",
        "index_options" : "offsets",
        "analyzer" : "english"
      }

Any pointers greatly appreciated...

1

There are 1 best solutions below

0
On

Your current query is doing a OR between your filter and multi-match query. Thats a reason you either get all documents.

I suppose you want to do AND between filter and multi-match query. If this is the case then following query works for me.

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "address1",
                        "fields": [
                            "name^10",
                            "address1",
                            "address2",
                            "address3",
                            "address4",
                            "address5",
                            "address6"
                        ],
                        "operator": "or"
                    }
                },
                {
                  "term": {
                    "exclude_from_search_results": {
                      "value": "false"
                    }
                  }
                }
            ]
        }
    },
    "aggs": {
        "types": {
            "terms": {
                "field": "name"
            }
        }
    }
}

Hope this help, Thanks.