Elasticsearch - range filter with multimatch?

旧巷老猫 提交于 2019-12-25 00:49:04

问题


I'm using elastic search to filter outfits:

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

end

The problem is I can't seem to filter them by how many times they were purchased like this:

      range: {
              purchased: {
                gte: 1000
             }
      }

Wherever I put this, it gives me the following error:

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

How do I write the range filter properly/put the code? Is there somewhere I can read more about this to understand how the hash should should be arranged?


回答1:


You can do it like this with a bool/filter query:

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


来源:https://stackoverflow.com/questions/46411276/elasticsearch-range-filter-with-multimatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!