0

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:

  • small shoes with glare
  • blue shoes
  • shoes

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

1 Answer 1

1

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
          ]
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

2 Comments

@FrakyDale please go through the answer, and let me know if this resolves your issue ?
soz, i didn't have time to try it until now :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.