【问题标题】:Elasticsearch string matching multiple fields in queryElasticsearch 字符串匹配查询中的多个字段
【发布时间】:2021-02-19 19:22:25
【问题描述】:

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

[match] 格式错误的查询,应为 [END_OBJECT],但找到了 [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')
      }
    }
  }
})

【问题讨论】:

  • 所有匹配和范围条件都需要包含在 bool/must 和/或 bool/filter 查询中

标签: javascript node.js elasticsearch elasticsearch-query elasticsearch-date


【解决方案1】:

一旦你 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')
            }
          }
        }
      ]
    }
  }
}

【讨论】:

  • 这似乎有效,虽然什么也没返回?这很奇怪,没有错误,但我确实在数据库中有数据
  • 先检查我对你之前问题的其他回答:)
猜你喜欢
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多