# Query for array matchings within multiple array

**URL:** <https://discuss.elastic.co/t/query-for-array-matchings-within-multiple-array/224152>\
**Category:** Elasticsearch\
**Created:** [March 18, 2020, 4:28pm UTC](https://discuss.elastic.co/t/query-for-array-matchings-within-multiple-array/224152 "2020-03-18T16:28:44Z")\
**Posts on this page:** 2\
**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:** [March 18, 2020, 4:28pm UTC](https://discuss.elastic.co/t/query-for-array-matchings-within-multiple-array/224152/1 "2020-03-18T16:28:44Z")

</div>

**Indexed documents**

```auto
{
  "book_id":"book01",
  "pages":[
    { "page_id":1, "words":["1", "2", "xx"] }
    { "page_id":2, "words":["4", "5", "xx"] }
    { "page_id":3, "words":["7", "8", "xx"] }
  ]
}
{
  "book_id":"book02",
  "pages":[
    { "page_id":1, "words":["1", "xx", "xx"] }
    { "page_id":2, "words":["4", "xx", "xx"] }
    { "page_id":3, "words":["7", "xx", "xx"] }
  ]
}

```

**Input data**

```auto
{
  "book_id":"book_new",
  "pages":[
    { "page_id":1, "words":["1", "2", "3"] }
    { "page_id":2, "words":["4", "5", "6"] }
    { "page_id":3, "words":["xx", "xx", "xx"] }
  ]
}

```

I have a number of books that have multiple pages. Each page has a list of words.  
I would like to search for books with more-than-threshold similar pages.

**Thresholds**

1. min\_word\_match\_score : 2 (minimum score of `words` match between two pages)
2. min\_page\_match\_score : 2 (minimum number of `similar pages` between two books)

**Key terms**

1. similar pages: Two pages that have at least `min_word_match_score` same words
2. similar book: Two books that have at least `min_page_match_score` similar pages

**Expected result**

Based on the specified thresholds, the correct return should be only `book01` because

1. book01-1 and book\_new-1 have score 2 (\>=min\_word\_match\_score, totalScore++)
2. book01-2 and book\_new-2 have score 2 (\>=min\_word\_match\_score, totalScore++)
3. book01 and book\_new have 2 total scores (totalScore \>= min\_page\_match\_score)

**Poor search query (not working)**

```auto
"bool" : {
   "should" : [
     {
        "match" : { "book_pages.visual_words" : {"query" : "1", "operator" : "OR"} },
        "match" : { "book_pages.visual_words" : {"query" : "2", "operator" : "OR"} },
        "match" : { "book_pages.visual_words" : {"query" : "3", "operator" : "OR"} }
     }
   ],
   "minimum_should_match" : 2
   "adjust_pure_negative" : true,
   "boost" : 1.0
 }
}

```

I first tried to make a part if the query for page match but it's not search array by array and it's just searching against words of all pages. And I am not really sure how to manage the two different scores - words-match-score and pages-match-score.

Should I dig into innerHit? Please help!

---

<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:** [April 15, 2020, 4:28pm UTC](https://discuss.elastic.co/t/query-for-array-matchings-within-multiple-array/224152/2 "2020-04-15T16:28:46Z")

</div>

Closed on April 15, 2020, 4:28pm UTC

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