# Bag-of-words matching

**URL:** <https://discuss.elastic.co/t/bag-of-words-matching/148628>\
**Category:** Elasticsearch\
**Created:** [September 14, 2018, 1:23pm UTC](https://discuss.elastic.co/t/bag-of-words-matching/148628 "2018-09-14T13:23:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christos\_Mantas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christos_mantas/32/17404_2.png) [@Christos\_Mantas](https://discuss.elastic.co/u/Christos_Mantas)\
**Post date:** [September 14, 2018, 1:23pm UTC](https://discuss.elastic.co/t/bag-of-words-matching/148628/1 "2018-09-14T13:23:24Z")

</div>

Hello.

there is a very simple thing I have been trying to do with ES, and only managed via a workaround:  
given a query and a document field, match all the query's terms with all the field's terms.  
In other words :

1. the query's terms are a subset of the field's
2. the field's terms are a subset of the query's

Requirement 1) is satisfied by using an `and` operator in a match query. But I have not been able to find a way for 2).

They only way I managed to achieve this so far is by leveraging `highlight` and requiring that all of the string is highlighted (in my application logic)

Is there another way?  
Example

```auto
PUT bananas/banana/1
{ "name" : "banana"}

PUT bananas/banana/2
{ "name" : "ripe banana"}

PUT bananas/banana/3
{ "name" : "not so ripe banana"}

GET bananas/_search
{
  "query" : {
    "match": { "_all": { "query": "ripe banana", "operator": "and" } }
  },
  "highlight" : {
    "fields" : { "*" : { } },
    "require_field_match": false
  }
}

# RETURNS (edited):

  "hits" : {
    "hits" : [
      {
        "_index" : "bananas",
        "_type" : "banana",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "not so ripe banana"
        },
        "highlight" : {
          "name" : [
            "not so <em>ripe</em> <em>banana</em>"
          ]
        }
      },
      {
        "_index" : "bananas",
        "_type" : "banana",
        "_id" : "2",
        "_score" : 0.51623213,
        "_source" : {
          "name" : "ripe banana"
        },
        "highlight" : {
          "name" : [
            "<em>ripe</em> <em>banana</em>"
          ]
        }
      }
    ]
  }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 12, 2018, 1:23pm UTC](https://discuss.elastic.co/t/bag-of-words-matching/148628/2 "2018-10-12T13:23:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
