# Similar document search?

**URL:** <https://discuss.elastic.co/t/similar-document-search/81893>\
**Category:** Elasticsearch\
**Created:** [April 11, 2017, 12:14am UTC](https://discuss.elastic.co/t/similar-document-search/81893 "2017-04-11T00:14:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![olalonde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalonde/32/17118_2.png) [@olalonde](https://discuss.elastic.co/u/olalonde)\
**Post date:** [April 11, 2017, 12:14am UTC](https://discuss.elastic.co/t/similar-document-search/81893/1 "2017-04-11T00:14:21Z")

</div>

I'm trying to do similar document search with ES.

Let's say I have the following documents:

```auto
Document 1: 0 0 0 0 1 1 3 3 4 4 4
Document 2: 0 1 1 3 4
Document 1: 0 0 0 0 0 0 0 0 0 0 1 1 3 3 4 4 4
Query Document: 0 0 0 0 0 1 1 3 3 4 4 4

```

If I use "Query Document" to search, the first result should be `Document 1` because their "bag of words" (term vectors) representation is the closest.

I was wondering whether this was possible with ES?

So far, this is what I've tried without much success:

Here's how I create the index:

```python
        settings = {
            'index': {
                'similarity': {
                    'my_similarity': {
                        'type': 'classic',
                    },
                },
            },
            'analysis': {
                'filter': {},
                'analyzer': {
                    'visual_word': {
                        'type': 'custom',
                        'tokenizer': 'whitespace',
                        'filter': [],
                        'similarity': 'my_similarity',
                    }
                }
            }
        }
        mappings = {
                doc_type: {
                    '_source': {
                        'enabled': False,
                    },
                    'properties': {
                        'filename': {
                            'type': 'keyword',
                            # 'index': 'not_analyzed',
                        },
                        'visual_words': {
                            'type': 'text',
                            'analyzer': 'visual_word',
                            'term_vector': 'yes',
                        },
                    },
                }
        }

            es.indices.create(
                    index = 'myindex',
                    body = {
                        'mappings': mappings,
                        'settings': settings,
                    },
            )

```

Here's how I do my search query:

```python
        query = {
            'query': {
                'match': {
                    'visual_words': {
                        'query': '0 0 0 0 0 1 1 3 3 4 4 4',
                    }
                }
            }
        }
        res = es.search(
                index = 'myindex',
                doc_type = 'image',
                body = query,
                explain = True,
        )

```

That strategy gives unexpected results. For example, querying an exact copy of a document in the index won't even return it as a first result.

---

<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:** [May 9, 2017, 12:17am UTC](https://discuss.elastic.co/t/similar-document-search/81893/2 "2017-05-09T00:17:35Z")

</div>

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