【问题标题】:elasticsearch search - search multiple fieldselasticsearch search - 搜索多个字段
【发布时间】:2021-12-06 19:26:45
【问题描述】:

我是弹性搜索的新手,我正在尝试搜索具有以下属性的索引:

// user index
{
  profile: {
    name: string,
    description: string,
    city: string
    state: string
  },
  services: [
    {
      serviceName: string
    },
    {
      serviceName: string
    },
    {
      serviceName: string
    },
    ...
  ]
}

我正在尝试合并我的 query_stringnested,但我似乎无法让它工作。

body: {
  query: {
    query_string: {
      query: `*${searchTerm}*`,
      fields: [
        // these profile fields work
        'profile.name',
        'profile.description',
        'profile.city',
        'profile.state',

        // doesnt work, need to use nested
        'services.serviceName'
      ]
    },

    // if i use nested here, elasticsearch throws error
    // can I combine these two queries like this?
    nested: {

    }
  }
}

我找不到任何同时具有 nestedquery_string 搜索查询的好示例。

大家有什么建议吗?

任何有帮助的东西,甚至是我正在尝试做的事情的优秀文档/示例的链接。

【问题讨论】:

    标签: elasticsearch search


    【解决方案1】:

    您可以使用布尔查询组合query_stringnested query

    {
      "query": {
        "bool": {
          "must": [
            {
              "query": {
                "query_string": {
                  "query": "search term",
                  "fields": []
                }
              }
            },
            {
              "query": {
                "nested": {
                  "path": "services",
                  "query": {
                    "services.serviceName": "blue"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 谢谢@ESCoder,有没有用于弹性搜索的 IRC 或聊天室?
    • 我收到[parsing_exception] Reason: unknown query [query]
    • 好的,我必须删除 "query_string""nested" 键的额外 "query" 父级
    • @Zac 好久不见了!如果它帮助您解决问题,您能否接受并投票赞成答案。 TIA ;-)
    猜你喜欢
    • 2018-12-22
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    相关资源
    最近更新 更多