phpelasticsearchmatchexact-match

Elastic - Only return fully matching documents


Having the following dataset:

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

Category's mapping is set to text.

And making a query like:

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

Elastic always returns me as first results/highest score:

Rather than shoe.

How can i tell elastic that i only want documents whose match is 100% of category's content? (not query)

I mean, if my search doesn't contain "blue shoes", i don't want blue result to appear. I only expected to get shoe or shoe and shoes at max.

I can't use term, because as i said, i don't expect a full match with the query. I expect the matched documents field to be fully matched.

Examples:

  • 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

Solution

  • As far as I know, there is no direct way to achieve your use case. You can use Percolate query to achieve your use case.

    Adding a working example with index data, mapping, search query, and search result

    Index Mapping:

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

    Index Data:

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

    Search Query:

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

    Search Result:

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