Elasticsearch:多前缀和多字段搜索问题

Elasticsearch: Issue with multiple prefix and multiple fields search

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

{ "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." }

我的 objective 是在他们的课程名称和工作角色字段中匹配“学士”和“工程”。使用下面的查询,不太确定为什么返回 2 个课程,但文档 ID 2 不满足条件。

如果我只搜索“课程名称”,它会按预期工作。在“职位”中搜索 returns 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"
                }
            }
        ]
    }
}

感谢您的帮助!

Query String Query will expand your query to a OR query for each field you provide. Please have a look 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"
            ]
          }
        }
      ]
    }
  }
}