elasticsearch - “match”查询以及“should”子句在Elasticsearch中提供了超出要求的匹配结果

标签 elasticsearch

我在elasticsearch中编写了以下Lucene查询,以获取带有Id字段的文档,如上所述:

GET requirements_v3/_search
  {
   "from": 0, 
   "size": 10, 
   "query": {
   "bool": {
  "filter": {
    "bool": {
      "should": [
    {"match": {
      "Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
    }},
    {
      "match": {
      "Id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69"
      }
    },
    {
      "match": {
      "Id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa"
      }
    },
    {
      "match": {
      "Id": "0aa1db52-c0fb-4bf6-9223-00edccc32703"
      }
    },
    {
      "match": {
      "Id": "8c399993-f273-4ee0-a1ab-3a85c6848113"
      }
    },
    {
      "match": {
      "Id": "4461eb37-487e-4899-a7be-914640fab0e0"
      }
    },
    {
      "match": {
      "Id": "07052261-b904-4bfc-a6fd-3acd28114c6a"
      }
    },
    {
      "match": {
      "Id": "95816ff0-9eae-4196-99fc-86c6f43395fd"
      }
    },
    {
      "match": {
      "Id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a"
      }
    },
    {
      "match": {
      "Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f"
      }
    }
  ]
 }
 }
 }     
}

以上查询的响应为:
 {
  "took": 14,
  "timed_out": false,
  "_shards": {
  "total": 5,
  "successful": 5,
  "skipped": 0,
"failed": 0
},
"hits": {
"total": 18,
"max_score": 0,
"hits": [
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
    "_score": 0,
    "_source": {
      "Id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "4461eb37-487e-4899-a7be-914640fab0e0",
    "_score": 0,
    "_source": {
      "Id": "4461eb37-487e-4899-a7be-914640fab0e0",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
    "_score": 0,
    "_source": {
      "Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
      "Name": "Create Configurator"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "d75d9a7c-e145-487e-922f-102c16d0026f",
    "_score": 0,
    "_source": {
      "Id": "d75d9a7c-e145-487e-922f-102c16d0026f",
      "Name": "Create Configurator"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
    "_score": 0,
    "_source": {
      "Id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
      "Name": "Detail Page - Build"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
    "_score": 0,
    "_source": {
      "Id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
    "_score": 0,
    "_source": {
      "Id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
      "Name": "HUC"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
    "_score": 0,
    "_source": {
      "Id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
      "Name": "DAMS UpsertUnenrollPrice"        }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
    "_score": 0,
    "_source": {
      "Id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
      "Name": "Item Setup"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
    "_score": 0,
    "_source": {
      "Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
      "Name": "Installments"        
   }
  }
 ]
}
}

这将totalHits称为“18”。为什么返回的项目多于10?我认为匹配查询应用于“完全匹配”,为什么还要在此处返回更多文档?

附注:我知道我可以使用Ids查询,但是我想知道为什么它没有返回正确的响应

更新:将大小设置为20将返回以下响应:
 {
  "took": 195,
  "timed_out": false,
  "_shards": {
  "total": 5,
 "successful": 5,
 "skipped": 0,
"failed": 0
},
"hits": {
 "total": 18,
 "max_score": 0,
 "hits": [
   {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
    "_score": 0,
    "_source": {
      "Id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "4461eb37-487e-4899-a7be-914640fab0e0",
    "_score": 0,
    "_source": {
      "Id": "4461eb37-487e-4899-a7be-914640fab0e0",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
    "_score": 0,
    "_source": {
      "Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
      "Name": "Create Configurator"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "d75d9a7c-e145-487e-922f-102c16d0026f",
    "_score": 0,
    "_source": {
      "Id": "d75d9a7c-e145-487e-922f-102c16d0026f",
      "Name": "Create Configurator"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
    "_score": 0,
    "_source": {
      "Id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
      "Name": "Detail Page - Build"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
    "_score": 0,
    "_source": {
      "Id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
    "_score": 0,
    "_source": {
      "Id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
      "Name": "HUC"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
    "_score": 0,
    "_source": {
      "Id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
      "Name": "DAMS UpsertUnenrollPrice"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
    "_score": 0,
    "_source": {
      "Id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
      "Name": "Item Setup"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
    "_score": 0,
    "_source": {
      "Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
      "Name": "Installments"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "b9437079-47c4-487e-abf0-1ff076f69e0f",
    "_score": 0,
    "_source": {
      "Id": "b9437079-47c4-487e-abf0-1ff076f69e0f",
      "Name": "Detail Page - Strings "
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "0aa1db52-c0fb-4bf6-9223-00edccc32703",
    "_score": 0,
    "_source": {
      "Id": "0aa1db52-c0fb-4bf6-9223-00edccc32703",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a",
    "_score": 0,
    "_source": {
      "Id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a",
      "Name": "Create Configurator"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "fd259359-4f6d-4530-ac29-fcebe00d66a6",
    "_score": 0,
    "_source": {
      "Id": "fd259359-4f6d-4530-ac29-fcebe00d66a6",
      "Name": "Invite Platform"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "1b2ba0bb-3e7f-46fb-b904-07460b84848b",
    "_score": 0,
    "_source": {
      "Id": "1b2ba0bb-3e7f-46fb-b904-07460b84848b",
      "Name": "Training"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "8c399993-f273-4ee0-a1ab-3a85c6848113",
    "_score": 0,
    "_source": {
      "Id": "8c399993-f273-4ee0-a1ab-3a85c6848113",
      "Name": "Configure ASIN for Reporting"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa",
    "_score": 0,
    "_source": {
      "Id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa",
      "Name": "Create Extended/Limited Warranty Configuration"
    }
  },
  {
    "_index": "requirements_v3",
    "_type": "_doc",
    "_id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69",
    "_score": 0,
    "_source": {
      "Id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69",
      "Name": "Invite Platform"
     }
    }
  ]
 }
}

最佳答案

让我们通过以下映射了解这一点:

{
  "_doc": {
    "properties": {
      "Id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

上面的映射是由elasticsearch动态创建的。现在让我们关注Id字段。它的类型是text。默认情况下,analyzer数据类型的textstandard分析器。当将此分析器应用于此字段的输入时,它将被标记为术语。因此,例如,如果您输入Id的值为33f87d98-024f-4893-aa1c-8d438a98cd1f,则会生成以下 token :
33f87d98
024f
4893
aa1c
8d438a98cd1f

如您所见,输入值被用作分隔符的-分割。这是因为在其上应用了standard analyzer
Id下还有一个子字段keyword,其类型是keyword。对于keyword类型,将对输入进行索引,而不进行任何修改。

现在让我们了解为什么要匹配更多文档并且结果计数超出预期。在查询中,您对match字段使用了Id查询,如下所示:
{
  "match": {
    "Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
  }
}

默认情况下,匹配查询使用与映射中的字段相同的分析器。因此,再次对查询中的Id值应用相同的分析器,并且以与上述类似的方式将输入拆分为 token 。在匹配查询输入字符串的标记之间应用的默认运算符为OR,因此您的查询实际上变为:
b8bf49a4 OR 960b OR 4fa8 OR 8c5f OR a3fce4b4d07b
如果上述任何标记与Id字段中存储的任何索引词匹配,则该文档被视为匹配。

基于上述映射的上述解决方案:

请改用关键字字段。因此查询变为:
{
  "match": {
    "Id.keyword": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
  }
}

有关match如何工作的更多信息see here

也正如@Curious_MInd在他的回答中提到的,使用terms比在match中使用多个should更好。

关于elasticsearch - “match”查询以及“should”子句在Elasticsearch中提供了超出要求的匹配结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53510502/

相关文章:

python - 如何在不完全匹配es 2.X的情况下获得成功? (Python弹性搜寻)

elasticsearch - 分片/副本设置可实现高可用性

elasticsearch - 无法在elasticsearch中搜索数组

elasticsearch - ES7 - 如何建模 1-n 父子关系 - 不同的 ES 类型

Elasticsearch - 一般架构和 Elastic Cloud 问题

ruby-on-rails - Rails Elasticsearch,Searchkick

elasticsearch - Filebeat 6.1.0 和 exclude_lines 选项

elasticsearch - 在 Elasticsearch 中更改集群名称

elasticsearch - Hibernate Search查询中的fullTextQuery.getResultList()出现com.google.gson.JsonArray.getAsString错误

java - 如何使用Java API编写此Elasticsearch查询