Elasticsearch字符串匹配查询中的多个字段

Elasticsearch string matching multiple fields in query

我正在尝试查询我的 Elastic Search 数据库并匹配多个字段,但是,我得到:

[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

这很奇怪,因为我肯定能够使用 match 然后我的字段名称来匹配一些文本吗?

我要查询的字段在我的 data 对象内,这是自定义的,并且由于添加(点)在这里不起作用,我将其包装起来,我的数据:

{
    "_index": "my_table",
    "_type": "_doc",
    "_id": "IVnGuXcBt3ZsPNCw23Eg",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "mappings": {
            "properties": {
                "period_from": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "fielddata": true
                },
                "period_to": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "fielddata": true
                },
                "created_at": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "fielddata": true
                },
                "updated_at": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "fielddata": true
                }
            }
        },
        "data": {
            "dataset": "events",
            "event_category": "test1",
            "event_action": "test2",
            "event_count": "199",
            "period_from": "2021-02-19 00:02:00",
            "period_to": "2021-02-19 10:02:29",
            "created_at": "2021-02-19 10:02:79",
            "updated_at": "2021-02-19 10:02:79"
        },
        "period_from": "2021-02-19 00:02:00",
        "period_to": "2021-02-19 10:02:29",
        "created_at": "2021-02-19 10:02:18",
        "updated_at": "2021-02-19 10:02:18"
    }
}

我在这里错过了什么:

events = await elastic.find('my_table', {
  query: {
    match: {
      'data.event_category': 'test1'
    },
    match: {
      'data.event_action': 'test2'
    },
    range: {
      period_from: {
        gte: moment(from).format('YYYY-MM-DD HH:MM:SS')
      },
      period_to: {
        lte: moment(to).format('YYYY-MM-DD HH:MM:SS')
      }
    }
  }
})

一旦您 correctly 设置了日期字段的映射,请使用:

{
  query: {
    bool: {
      must: [
        {
          match: {
            "data.event_action": "test2"
          }
        },
        {
          range: {
            period_from: {
              gte:  moment(from).format('YYYY-MM-DD HH:MM:SS')
            }
          }
        },
        {
          range: {
            period_to: {
              lte: moment(to).format('YYYY-MM-DD HH:MM:SS')
            }
          }
        }
      ]
    }
  }
}