Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 363
neel Avatar asked Feb 15 '18 03:02

neel


People also ask

How do I view documents in Elasticsearch?

You can view the document in two ways. The Table view displays the document fields row-by-row. The JSON (JavaScript Object Notation) view allows you to look at how Elasticsearch returns the document. The link is valid for the time the document is available in Elasticsearch.

What is Elasticsearch similarity?

A similarity (scoring / ranking model) defines how matching documents are scored. Similarity is per field, meaning that via the mapping one can define a different similarity per field.

What is Elasticsearch matching?

The match query is of type boolean . It means that the text provided is analyzed and the analysis process constructs a boolean query from the provided text. The operator parameter can be set to or or and to control the boolean clauses (defaults to or ).


1 Answers

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

like image 91
David Corp Avatar answered Sep 20 '22 17:09

David Corp