elasticsearchopensearch

How to improve score when having a exact match in a list on elasticsearch?


I'm pretty new to elastic search, I'm having this issue below.

Having these two records:

    POST test/_doc/1
    {
      "id": 1,
      "authors": [
        {
          "name": "Test Name",
          "url": "/url/1/"
        }
      ]
    }

    POST test/_doc/2
    {
      "id": 2,
      "authors": [
        {
          "name": "Test Name",
          "url": "/url/1/"
        },
            {
          "name": "Another author",
          "url": "/url/another/"
        }
      ]
    }

And this query:

    GET test/_search
    {
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "should": [
                {
                  "match_phrase": {
                    "authors.name": {
                      "_name": "exact match in authors",
                      "query": "Test Name",
                      "boost": 100,
                      "slop": 1
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }

Why the score is lower when having multiple authors? And how can I make it higher or the same value as the record that have only one author?

    {
      ...
      "hits": {
        "max_score": 42.221836,
        "hits": [
          {
            "_score": 42.221836,
            "_source": {
              "id": 1,
              "authors": [
                {
                  "name": "Test Name",
                  "url": "/url/1/"
                }
              ]
            },
            "matched_queries": [
              "exact match in authors"
            ]
          },
          {
            "_score": 32.088596,
            "_source": {
              "id": 2,
              "authors": [
                {
                  "name": "Test Name",
                  "url": "/url/1/"
                },
                {
                  "name": "Another author",
                  "url": "/url/another/"
                }
              ]
            },
            "matched_queries": [
              "exact match in authors"
            ]
          }
        ]
      }
    }

I can't find anything on the docs about this.

The following details are just to make sure stackoverflow don't show the following error: It looks like your post is mostly code; please add some more details.


Solution

  • I tried @paulo solution, but it didn't fully work for me, so I ended up adding a nested field:

          "authors": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "url": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
    

    And used this query:

    {
        "nested": {
            "path": "authors",
            "_name": "exact match in authors",
            "query": {
                "bool": {
                    "must": {
                        "match_phrase": {
                            "authors.name": {
                                "query": "Test Name",
                                "boost": 100,
                                "slop": 1,
                            }
                        }
                    }
                }
            },
        }
    }
    

    Elastic search doc: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

    After these changes, it worked perfectly!