查询特定字段时输出为空

我想从我的ElasticSearch索引中获取数据。

所以我有以下索引:

使用Node,如果我这样做:

const hits = await elasticClient.search({
    index: esIndex,size: 999,sort: 'ts:desc',body: {
      query: {
        bool: {
          must: [
            {
              term: {
                userId: '56',},}
          ],});

它将返回:

[
    {
        "_index": "events_rc","_type": "_doc","_id": "tf-szm0B_tB6kax4xmnr","_score": null,"_source": {
            "userId": "56","eventName": "synchronizationStart","ts": 1571130486383
        }
    },{
        "_index": "events_rc","_id": "tP-szm0B_tB6kax4xmnr","eventName": "showSynchronizationmodal","ts": 1571130447209
        }
    }
]

但是如果我执行以下查询:

const hits = await elasticClient.search({
    index: esIndex,body: {
      query: {
        bool: {
          must: [
            {
              term: {
                eventName: 'synchronizationStart',});

它将返回一个空数组...

我想知道为什么它可以找到与userId而不是eventName的匹配项?

两个字段的映射似乎相同

"eventName": {
  "type": "text","fields": {
  "keyword": {
    "type": "keyword","ignore_above": 256
  }
}
"userId": {
  "type": "text","ignore_above": 256
  }
}

thx

largehand 回答:查询特定字段时输出为空

您需要在eventName.keyword字段上进行查询,它将起作用:

const hits = await elasticClient.search({
    index: esIndex,size: 999,sort: 'ts:desc',body: {
      query: {
        bool: {
          must: [
            {
              term: {
                'eventName.keyword': 'synchronizationStart',},}
          ],});
,

事件名称的类型为文本。您应该使用匹配而不是术语。

  

避免对文本字段使用术语查询。

取自Here

如果您将使用 eventName.keyword ,则必须在字段中添加自定义规范化器,否则必须搜索 exact 字词。

更改

    const hits = await elasticClient.search({
    index: esIndex,body: {
      query: {
        bool: {
          must: [
            {
              term: {
                eventName: 'synchronizationStart',});

收件人:

const hits = await elasticClient.search({
index: esIndex,body: {
  query: {
    bool: {
      must: [
        {
          match: {
            eventName: 'synchronizationStart',}
      ],

});

本文链接:https://www.f2er.com/3150979.html

大家都在问