【问题标题】:Elasticsearch: Issue with multiple prefix and multiple fields searchElasticsearch:多个前缀和多个字段搜索的问题
【发布时间】:2021-06-21 08:35:51
【问题描述】:

希望有人能在这方面启发我。假设我有以下数据:

{ "index": { "_index": "courses_test", "_id": 1 } }
{ "Course Name": "Bachelor of Arts in Music", "Job Role": "Theatre & Media Director, Video Engineer" }
{ "index": { "_index": "courses_test", "_id": 2 } }
{ "Course Name": "Bachelor of Arts in Engineering", "Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant." }

我的目标是在他们的课程名称和工作角色字段中匹配“学士”和“工程”。通过下面的查询,不太清楚为什么返回了 2 个课程,但文档 ID 2 不满足条件。

如果我只在“课程名称”中搜索,它会按预期工作。在“工作角色”中搜索返回 0,这也是正确的。

我正在使用查询字符串并使用 *,这样即使用户只是输入了前缀,例如'bach eng',它应该仍然匹配。

完整查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "Bachelor* AND Engineer*",
                        "fields": [
                            "Course Name",
                            "Job Role"
                        ]
                    }
                }
            ]
        }
    }
}

回复:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 2.0,
        "hits": [
            {
                "_index": "courses_test",
                "_type": "_doc",
                "_id": "1",
                "_score": 2.0,
                "_source": {
                    "Course Name": "Bachelor of Arts in Music",
                    "Job Role": "Theatre & Media Director, Video Engineer"
                }
            },
            {
                "_index": "courses_test",
                "_type": "_doc",
                "_id": "2",
                "_score": 2.0,
                "_source": {
                    "Course Name": "Bachelor of Arts in Engineering",
                    "Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant"
                }
            }
        ]
    }
}

感谢您的帮助!

【问题讨论】:

  • 所以我在这里问了这个问题:discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504,只是为了更新这个问题,我找到了满足我目标的解决方案。在下面从@ibexit re AND 到 OR 的解释之后。我想过将每个字段的查询分开,并使用“应该”而不是“必须”。

标签: elasticsearch query-string


【解决方案1】:

Query String Query 会将您的查询扩展为您提供的每个字段的 OR 查询。请看here。最后,所有文档将匹配在任何字段中至少有一个匹配项。

您可能需要使用https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html AND/OR https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html 重写查询


为了将来的调试:有一个 API 端点能够解释文档匹配的原因:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

在您的情况下,这应该会给您相关的见解(请注意 url 中的索引名称和文档 ID):

GET /courses_test/_explain/1  
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "Bachelor* AND Engineer*",
            "fields": [
              "Course Name",
              "Job Role"
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

  • 我忘了检查那个文档,只关注 opendistro 文档,但是对 query_string 的引用在那里更有意义!谢谢!而且我遇到了解释,但总是返回“没有匹配的子句”。但再次感谢您的意见!
猜你喜欢
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2014-01-16
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多