neel
neel

Reputation: 9061

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?

Upvotes: 11

Views: 9295

Answers (3)

David Corp
David Corp

Reputation: 506

{
            "query": {
                "more_like_this" : {
                    "fields" : ["first name", "last name", "address", "etc"],
                    "like" : "your_query",
                    "min_term_freq" : 1,
                    "max_query_terms" : 12
                }
            }

 }  

Put everything in your_query . You can increase or decrease min_term_freq and max_query_terms

Upvotes: 1

David Corp
David Corp

Reputation: 506

{
    "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

Upvotes: 8

arun
arun

Reputation: 11023

You haven't mentioned the types of your fields. A general approach is to use a catch all field (using copy_to) with the more like this query.

Upvotes: 2

Related Questions