0

使用 Elasticsearch 2 和 Rails 4,使用 elasticsearch-model gem

一切都很好,甚至地理点距离也在工作。但是,我一生都无法弄清楚如何使简单的布尔过滤器起作用。我有一个简单的布尔值“exclude_from_search_results”(如果为真),应该会导致从结果中过滤掉记录。

这是我在 rails 控制器中的查询(没有过滤器):

    @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'}}}
  )

我已经在 bool 或 query 部分或外部添加了它,但我要么没有得到任何文档,要么得到所有文档。(9000 应该匹配)

例子:

  @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'}}}
  )

我还尝试将过滤器子句放在不同的位置,但要么出错,要么没有结果。我究竟做错了什么??可能缺少一些简单的东西......

这是我的映射:

    "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"
      }

任何指针都非常感谢...

4

1 回答 1

1

您当前的查询在您的过滤器和多匹配查询之间进行 OR。这就是您获得所有文件的原因。

我想你想在过滤器和多匹配查询之间做 AND。如果是这种情况,那么以下查询对我有用。

{
    "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"
            }
        }
    }
}

希望这有帮助,谢谢。

于 2017-01-01T07:17:10.190 回答