【问题标题】:Why is my ElasticSeach query returning zero document?为什么我的 ElasticSearch 查询返回零文档?
【发布时间】:2020-09-13 10:23:37
【问题描述】:

我正在尝试从 Lambda 工作人员查询 AWS ElasticSearch 域。

为此,我使用http-aws-es 和用于 Elastic Search 的主要 javascript 客户端。

我查询具有以下相关字段的文档:

  • ref 字段 - 字符串
  • status 字段 - 字符串枚举(REMOVEDBLOCKEDPUBLISHEDPENDINGVERIFIED
  • field 字段 - 字符串数组
  • thematics 字段 - 字符串数组

我想要实现的是:

  1. 过滤所有不是PUBLISHEDVERIFIED 或设置了ref 字段的文档
  2. 返回与keywwords 参数(字符串数组)相对于fieldthematics 中的值的最佳匹配
  3. 排序以将具有PUBLISHED 状态的文档放在首位
  4. 将结果数限制为 20 个

我找到了more_like_this 运算符,并试了一下。我一步一步构建我的查询,至少实际版本不会返回错误,但不会返回任何文档。它仍然错过了上面的 ref 过滤器 + #3 和 #4。这是查询:

  const client = new elasticsearch.Client({
      host: ELASTICSEARCH_DOMAIN,
      connectionClass: httpAwsEs,
      amazonES: {
        region: AWS_REGION,
        credentials: new AWS.EnvironmentCredentials('AWS')
      }
    })
    let keywords = event.arguments.keywords
    let rst = await client.search({
      body: {
        'query': {
          'bool': {
            'filter': {
              'bool': {
                'must_not': [
                  {
                    'term': {
                      'status': 'REMOVED'
                    }
                  },
                  {
                    'term': {
                      'status': 'PENDING'
                    }
                  },
                  {
                    'term': {
                      'status': 'BLOCKED'
                    }
                  }
                ]
              }
            },
            'must': {
              'more_like_this': {
                'fields': ['field', 'thematics'],
                'like': keywords,
                'min_term_freq': 1,
                'max_query_terms': 2
              },
              'should': [
                {
                  'term': {
                    'status': 'PUBLISHED'
                  }
                }
              ]
            }
          }
        }
      }

    })
    console.log(rst)
    return rst

我必须上传我的 lambda 代码来调试它,它使调试变得非常复杂。由于我以前从未进行过 ES 查询,因此我想至少获得一些关于如何进行此操作的提示,或者知道我是否误用了 ES 查询语法。


编辑:

根据要求,这是我的索引映射(JS类型):

  • 城市文本(字符串)
  • contact_email 文本(字符串)
  • contact_entity 文本(字符串)
  • contact_firstname 文本(字符串)
  • contact_lastname 文本(字符串)
  • 联系人文本(字符串列表)
  • 国家文本(字符串)
  • createdAt 日期(字符串)
  • 描述文本(字符串)
  • editKey 文本(字符串)
  • 字段文本(字符串)
  • id 文本(字符串)
  • 名称文本(字符串)
  • pubId 文本(字符串)
  • 参考文本(字符串)
  • 状态文本(字符串)
  • 状态文本(字符串)
  • 主题文本(字符串数组)
  • 输入文本(字符串数组)
  • updatedAt(字符串)
  • 网址文本(字符串)
  • verifyKey 文本(字符串)
  • 区域文本(字符串数组)

取自 AWS 弹性搜索管理控制台(索引选项卡 > 映射)

【问题讨论】:

    标签: javascript elasticsearch morelikethis


    【解决方案1】:

    您的查询中有一个或两个问题(must 中的shouldfilter 中的must_not)。请尝试以下简化查询:

    {
      'query': {
        'bool': {
          'must_not': [
            {
              'term': {
                'status.keyword': 'REMOVED'
              }
            },
            {
              'term': {
                'status.keyword': 'PENDING'
              }
            },
            {
              'term': {
                'status.keyword': 'BLOCKED'
              }
            }
          ],
          'must': [
            {
              'more_like_this': {
                'fields': [
                  'field',
                  'thematics'
                ],
                'like': keywords,
                'min_term_freq': 1,
                'max_query_terms': 2
              }
            }
          ],
          'should': [
            {
              'term': {
                'status.keyword': 'PUBLISHED'
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 非常感谢您让我走上正轨。你为我节省了很多时间。干杯!
    • 太棒了,很高兴它有帮助!
    • 查询结果中没有考虑must_not。我得到带有禁止值的文档(例如状态:'BLOCKED')。如果我将其替换为filter,则不会返回任何结果。 ES 版本是 6.2。我还尝试用match_phrase 替换more_like_this,但没有成功。有什么建议吗?我应该打开另一个问题吗?再次感谢
    • 你能显示你的索引映射吗?我可能知道原因
    • 我需要来自 ES 的映射,即你从GET your-index-name/_mapping得到的映射
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多