Search code examples
elasticsearchsimilarity

How to find similar documents in Elasticsearch


My documents are made up of using various fields. Now given an input document, I want to find the similar documents using the input document fields. How can I achieve it?


Solution

  • {
        "query": {
            "more_like_this" : {
                "ids" : ["12345"],
                "fields" : ["field_1", "field_2"],
                "min_term_freq" : 1,
                "max_query_terms" : 12
            }
        }
    
    }
    

    you will get similar documents to id 12345. Here you need to specify only ids and field like title, category, name, etc. not their values.

    Here is another code to do without ids, but you need to specify fields with values. Example: Get similar documents which have similar title to: elasticsearch is fast

    {
        "query": {
            "more_like_this" : {
                "fields" : ["title"],
                "like" : "elasticsearch is fast",
                "min_term_freq" : 1,
                "max_query_terms" : 12
            }
        }
    
    }
    

    You can add more fields and their values