Home > Software design >  How to force an Elasticsearch more_like_this query to include the source document?
How to force an Elasticsearch more_like_this query to include the source document?

Time:01-25

I have the following query:

{
  "query": {
    "more_like_this": {
      "fields": ["Slug", "Title"],
      "like": [
        {
          "_index": "articles",
          "_id": 12345
        }
      ],
      "min_term_freq": 1,
      "max_query_terms": 10,
      "min_doc_freq": 1,
      "include": true
    }
  }
}

My goal is to always include the source document (with the ID 12345). I'm using the include option, but that doesn't necessarily mean it'll always include my document. I want to do this so that I can avoid requesting my content twice, I can have my article the related articles in one request.

CodePudding user response:

You can use bool queries to achieve what you are looking for.

POST /[indexnames]/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "more_like_this": {
            "fields": [
              "Slug",
              "Title"
            ],
            "like": [
              {
                "_index": "articles",
                "_id": 12345
              }
            ],
            "min_term_freq": 1,
            "max_query_terms": 10,
            "min_doc_freq": 1,
            "include": true
          }
        },
        {
          "term": {
            "_id": "12345"
          }
        }
      ]
    }
  }
}

If you want to get the document on top of your list (to be ranked higher) you can use PinnedQuery like this:

GET /_search
{
  "query": {
    "pinned": {
      "ids": [
        "12345"
      ],
      "organic": {
        "bool": {
          "should": [
            {
              "more_like_this": {
                "fields": [
                  "Slug",
                  "Title"
                ],
                "like": [
                  {
                    "_index": "articles",
                    "_id": 12345
                  }
                ],
                "min_term_freq": 1,
                "max_query_terms": 10,
                "min_doc_freq": 1,
                "include": true
              }
            },
            {
              "term": {
                "_id": "12345"
              }
            }
          ]
        }
      }
    }
  }
}
  •  Tags:  
  • Related