如何在Elasticsearch中合并范围和多重匹配查询

时间:2019-07-05 12:47:11

标签: javascript elasticsearch kibana elasticsearch-painless

我在Elasticsearch文档中有字段。哪个有日期字段和两个字符串字段。我想在日期上应用范围,在两个字符串字段上使用匹配过滤器。如何应用?

假设我有这个文档

movies
{
  name    :"titanic",
  director:"james Cameron",
  releaseDate:"06-07-1997"
}

2 个答案:

答案 0 :(得分:0)

尝试这样的事情

query: {
          bool: {
            filter: [
              {
                multi_match: {
                  query: query,
                  fields: ['description^30','material^10']
                }
              },
              {
                range: {
                  purchased: { gte: 1000}
                }
              }
            ]
          }
        },
        field_value_factor: {
          field: "purchased"
        }
      }

答案 1 :(得分:0)

带有过滤器的布尔查询怎么样? 修改,因为请求了多次匹配

PUT /movies
{
  "mappings": {
    "properties": {
      "director": {
        "type": "text"
      },
      "name": {
        "type": "text"
      },
      "releaseDate": {
        "type": "date",
        "format": "dd-MM-yyyy"
      }
    }
  }
}

PUT movies/_doc/1
{
    "name": "titanic",
    "director": "james Cameron",
    "releaseDate": "06-07-1997"
}

GET /movies/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "releaseDate": {
              "gte": "05-07-1990",
              "lte": "05-07-2000"
            }
          }
        },
        {
          "multi_match": {
            "query": "james",
            "fields": ["director", "name"]
          }
        }
      ]
    }
  }
}

编辑:根据评论,您也可以进行词条查询而不是多重匹配:

GET /movies/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "releaseDate": {
              "gte": "05-07-1990",
              "lte": "05-07-2000"
            }
          }
        },
        {
          "term": {
            "name": "titanic"
          }
        },
        {
          "term": {
            "director": "james cameron"
          }
        }
      ]
    }
  }
}