弹性 - 只有 return 个完全匹配的文档

Elastic - Only return fully matching documents

具有以下数据集:

[
{
 "id": 234,
 "category": "shoe"
},
{
 "id": 44,
 "category": "shoes"
},
{
 "id": 49,
 "category": "blue shoes"
},
{
 "id": 48,
 "category": "small shoes with glare"
}
]

类别的映射设置为文本。

并进行如下查询:

$query = (new ElasticQuery())
            ->setQuery([
                "bool" => [
                    "must" => [
                        "match" => [
                            "category" => [
                                "query" => "shoe size 22"
                            ],
                        ]
                    ]
                ],
            ]);

弹性总是returns我是第一个results/highest得分:

而不是鞋子。

我如何告诉 elastic 我只想要匹配类别内容 100% 的文档? (不是查询)

我的意思是,如果我的搜索不包含“蓝色鞋子”,我不希望出现蓝色结果。我只希望得到最多的鞋子或鞋子和鞋子。

我不能使用术语,因为正如我所说,我不希望与查询完全匹配。我希望匹配的文档字段完全匹配。

示例:

  • Query: shoe size 22
  • Expected results:
  • shoe
  • Query: small shoes with glare
  • Expected results:
  • small shoes with glare
  • shoes
  • Query: blue shoes
  • Expected results:
  • blue shoes
  • shoes
  • Query: green shoes
  • Expected results:
  • shoes

据我所知,没有直接的方法来实现您的用例。您可以使用 Percolate query 来实现您的用例。

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "category": {
        "type": "text"
      },
      "query": {
        "type": "percolator"
      }
    }
  }
}

索引数据:

{
  "query": {
    "match": {
      "category": {
        "query": "shoe"
      }
    }
  }
}
{
  "query": {
    "match": {
      "category": {
        "query": "shoes"
      }
    }
  }
}
{
  "query": {
    "match": {
      "category": {
        "query": "blue shoes",
        "operator": "AND"
      }
    }
  }
}
{
  "query": {
    "match": {
      "category": {
        "query": "small shoes with glare
",
        "operator": "AND"
      }
    }
  }
}

搜索查询:

    {
      "query": {
        "percolate": {
          "field": "query",
          "document": {
            "category": "green shoes"
          }
        }
      }
    }

搜索结果:

"hits": [
      {
        "_index": "65787899",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.13076457,
        "_source": {
          "query": {
            "match": {
              "category": {
                "query": "shoes",
                "operator": "AND"
              }
            }
          }
        },
        "fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      }
    ]