# Longest match in string/array

**URL:** <https://discuss.elastic.co/t/longest-match-in-string-array/98121>\
**Category:** Elasticsearch\
**Created:** [August 23, 2017, 7:09pm UTC](https://discuss.elastic.co/t/longest-match-in-string-array/98121 "2017-08-23T19:09:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prarabdh9909](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@prarabdh9909](https://discuss.elastic.co/u/prarabdh9909)\
**Post date:** [August 23, 2017, 7:09pm UTC](https://discuss.elastic.co/t/longest-match-in-string-array/98121/1 "2017-08-23T19:09:27Z")

</div>

I am planning to use ES for storing tracebacks of exceptions such as:  
[a/b/c/d/func/line, e/f/g/h/func/line, i/j/k/l/func/line]

Now, for any incoming tracebacks, I want to rank the results based on which document matches call chain the most and also order matters (starting from reverse because doesn't matter if top half of call chain matches and rest doesn't)

So I have thought of something and I wanted to find out if there is something better I can use.

1. Store tracebacks as string and use match\_phrase query like:  
"a/b/c/d/func/line, e/f/g/h/func/line, i/j/k/l/func/line"  
If that doesn't return anything, look for:  
"e/f/g/h/func/line, i/j/k/l/func/line"  
and so on..  
This should make sure that docs with longest call chain match are at top.

I saw this thread suggesting to implement my own similarity model.

> [@Similarity score in array](https://discuss.elastic.co/t/similarity-score-in-array/13714):
>
> Hi , I have a field called tags which is an array of elements and i have applied a flat analyzer over it. Now i want a array of tags and i want to see which feed matches the array of tags the most. The logic to similarity is that The feed with maximum number of matching tags should come first. In case if 2 feeds have same amount of matched tag , then the feed with the highest percentage of matched tag should come. In short if my query is "terms" : { "tags" : ["one" , "two"] } , then t…

I am still a newbie to ES so I think that will take me a lot of time.

Is there anything out of the box that can help me?  
Or may be reduce the computation on ES side by something like stop on first match?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [August 23, 2017, 9:22pm UTC](https://discuss.elastic.co/t/longest-match-in-string-array/98121/2 "2017-08-23T21:22:58Z")

</div>

I don't think a custom similarity would help. I think I would do it the following way:

At index-time, make sure to map `trace` as a `keyword` field and pre-process values to store every suffix. For instance a/b/c/func/line would be indexed as

```auto
{
  "trace": ["line", "func/line", "c/func/line", "b/c/func/line", "a/b/c/func/line"]
}

```

Then at search time do the same splitting. For instance if your query is "d/c/func/line"

```auto
GET _search
{
  "query": {
    "bool": {
      "should": [
        { "term": { "trace": "func/line" } },
        { "term": { "trace": "c/func/line" } },
        { "term": { "trace": "d/c/func/line" } }
      ]
    }
  }
}

```

---

<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:** [September 20, 2017, 9:23pm UTC](https://discuss.elastic.co/t/longest-match-in-string-array/98121/3 "2017-09-20T21:23:40Z")

</div>

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