Elasticsearch - 具有多匹配的范围过滤器?

时间:2017-09-25 17:51:38

标签: ruby-on-rails elasticsearch search filter

我使用弹性搜索来过滤服装:

  def self.search(query)
  __elasticsearch__.search(
  {
    query: {
      function_score: {
        query: {
          multi_match: {
            query: query,
            fields: ['description^30','material^10']
          },
        },
        field_value_factor: {
          field: "purchased"
        },
      }
    }
  }
)

问题是我似乎无法按照这样购买它们的次数来过滤它们:

      range: {
              purchased: {
                gte: 1000
             }
      }

无论我把它放在哪里,都会给我以下错误:

[400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":88}],"type":"parsing_exception","reason":"[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":88},"status":400}

如何正确编写范围过滤器/放置代码?有什么地方我可以阅读更多关于这个,以了解应该如何安排哈希?

1 个答案:

答案 0 :(得分:0)

您可以使用bool/filter查询执行此操作:

  def self.search(query)
  __elasticsearch__.search(
  {
    query: {
      function_score: {
        query: {
          bool: {
            filter: [
              {
                multi_match: {
                  query: query,
                  fields: ['description^30','material^10']
                }
              },
              {
                range: {
                  purchased: { gte: 1000}
                }
              }
            ]
          }
        },
        field_value_factor: {
          field: "purchased"
        }
      }
    }
  }
)