# Question about backward matching query

**URL:** <https://discuss.elastic.co/t/question-about-backward-matching-query/179576>\
**Category:** Elasticsearch\
**Created:** [May 3, 2019, 7:04pm UTC](https://discuss.elastic.co/t/question-about-backward-matching-query/179576 "2019-05-03T19:04:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devon\_Yoo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/devon_yoo/32/45284_2.png) [@Devon\_Yoo](https://discuss.elastic.co/u/Devon_Yoo)\
**Post date:** [May 3, 2019, 7:04pm UTC](https://discuss.elastic.co/t/question-about-backward-matching-query/179576/1 "2019-05-03T19:04:17Z")

</div>

Hi,

**Indexed data in ES**

```auto
id(text) | words(keyword) 
A | "1 2 3 4 100 101 102 103 104 105"
B | "1 2 3 100 101 102 103 104 105"
C | "1 2 100"

```

**Search query**

```auto
"bool": { 
  "should": [
    { "term": {"words":"1"}},
    { "term": {"words":"2"}},
    { "term": {"words":"3"}},
    { "term": {"words":"4"}}
  ]
},
"_source": ["id"],
"size":1,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]

```

**Forward/Backward matching**

```auto
Source words: 15
Target words: 10
Matchings: 5
Forward Matching Rate: 33%(5/15)
Backward Matching Rate: 50%(5/10)

```

The search query returns an id that has the highest **forward matching** score. So the result of this query will be **"id:A"** because A has all of 1,2,3 and 4, and **its forward matching rate is 4/4**. **But its backward matching rate is 4/10.**

What I am trying to do is getting an id that has the highest **backward matching** rate. In other words, I am looking for **"id:C"** as **its backward matching is 2/3** which is the highest backward matching rate.

Does anyone know how to do it?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 6, 2019, 4:38pm UTC](https://discuss.elastic.co/t/question-about-backward-matching-query/179576/2 "2019-05-06T16:38:58Z")

</div>

It's a tricky ask since inverted indices don't quite work in this manner, so it's not easy to express with a simple query.

The simplest way to do something like this is to include another field which is the length of the first field. Then you can use that to help score the match, since you can check for matches in `words`, but also adjust the score because the `length_words` is `10` and you're only asking for `4` terms.

It's not perfect (doesn't help with duplicates), and it's also finicky to get working right.

You could probably cobble something together with a `shingle` tokenizer too, and look for exact shingle matches. Or ngrams.

---

<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:** [June 3, 2019, 4:39pm UTC](https://discuss.elastic.co/t/question-about-backward-matching-query/179576/3 "2019-06-03T16:39:00Z")

</div>

Closed on June 3, 2019, 4:39pm UTC

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