# Strategy for matching unstructured text to phrases in index

**URL:** https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583
**Category:** Elasticsearch
**Created:** [July 4, 2023, 5:34pm UTC](https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583 "2023-07-04T17:34:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rustunooldu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rustunooldu/32/122958_2.png) [@rustunooldu](https://discuss.elastic.co/u/rustunooldu)
#### Post date: [July 4, 2023, 5:34pm UTC](https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583/1 "2023-07-04T17:34:44Z")

</div>

I'm trying to extract data from product descriptions, and I have the catalog data indexed and categorized. For example, I have a color name index (that contains all possible colors for the product), and I want to be able to find the best color match for given query string:

> This is a 2017 pathfinder in beautiful **arctic blue** trim.

In other words, rather than searching for a phrase in indexed documents, phrases themselves are in the index, and my query input is the full text. What would be the best approach to solve this? Sounds pretty easy at first glance, but so far I couldn't find a query that works for me.

- Standard **match** doesn't do a good job because it's too broad; matches a bunch of random stuff that affect the score.
- Tried tokenizing the input and match multiple terms, using **span\_near** query to keep the term order, but it tries to match _all_ terms thus returns no result. For example:

```auto
  "query": {
    "span_near": {
      "clauses": [
        { "span_term": { "name": "this" } },
        { "span_term": { "name": "2017" } },
        { "span_term": { "name": "pathfinder" } },
        { "span_term": { "name": "beautiful" } },
        { "span_term": { "name": "arctic" } },
        { "span_term": { "name": "blue" } },
        { "span_term": { "name": "trim" } }
      ],
      "slop": 0,
      "in_order": true
    }
  }

```

- **more\_like\_this** looks promising, but it has limited config options and lucene scoring formula acts against me (for example in this specific case, there is actually a color named "beautiful blue", which scores higher than "arctic blue").

```auto
  "query": {
    "more_like_this" : {
      "fields" : ["color"],
      "like" : "This is a 2017 pathfinder in beautiful arctic blue trim.",
      "min_term_freq" : 1
    }
  }

```

I feel like I'm missing something; because this should be a simple substring search. What I need is the exact functionality of **match\_phrase** , except query and index are inversed.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [July 4, 2023, 6:26pm UTC](https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583/2 "2023-07-04T18:26:14Z")

</div>

Hi @rustunooldu

Not my area of expertise but I think perhaps you may be describing a percolator query

> **[Percolate query | Elasticsearch Guide \[8.8\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html)**

---

<div class="post-metadata">

### Author: ![rustunooldu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rustunooldu/32/122958_2.png) [@rustunooldu](https://discuss.elastic.co/u/rustunooldu)
#### Post date: [July 4, 2023, 7:39pm UTC](https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583/3 "2023-07-04T19:39:59Z")

</div>

Hi @stephenb  
I actually saw percolate query when I was looking for a solution, but I must have misinterpreted how it works, and I didn't pay much attention to it. Turns out it's exactly what I needed, thank you for the suggestion.

---

<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: [August 1, 2023, 7:40pm UTC](https://discuss.elastic.co/t/strategy-for-matching-unstructured-text-to-phrases-in-index/337583/4 "2023-08-01T19:40:07Z")

</div>

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