# Exact match of keywords & score weight keywords

**URL:** <https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900>\
**Category:** Elasticsearch\
**Created:** [November 22, 2018, 2:55pm UTC](https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900 "2018-11-22T14:55:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![basvd](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@basvd](https://discuss.elastic.co/u/basvd)\
**Post date:** [November 22, 2018, 2:55pm UTC](https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900/1 "2018-11-22T14:55:49Z")

</div>

Two small questions:

1. The first document has a match on "java-8", but I would only like to match on "java". Same for the second document with docker.  
How can I filter on the exact keywords? Should I change the mapping or is possible to fix this inside the search query?

2. It looks like the score also weights the number of (non-matching) tags. It possible to exclude the non-matching tags in the score?  
For me it is okay if ['php','java','x'] and ['php,','java','x','y','z'] have exactly the same score.

Request

```
GET localhost:9200/profile/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tags": "docker"
          }
        },
        {
          "term": {
            "tags": "php"
          }
        },
        {
          "term": {
            "tags": "java"
          }
        }
      ]
    }
  }
}

```

Response

```
[
  {
    "_score": 10.028147,
    "_source": {
      "name": "Harald",
      "tags": [
        "docker",
        "java-8",
        "website"
      ]
    }
  },
  {
    "_score": 9.958822,
    "_source": {
      "name": "Alex",
      "tags": [
        "java",
        "spring",
        "yaml",
        "spring-boot",
        "website",
        "docker",
        "docker-compose",
        "docker-machine"
      ]
    }
  },
  {
    "_score": 9.919757,
    "_source": {
      "name": "Fleming",
      "tags": [
        "java",
        "docker",
        "dockerfile",
        "website"
      ]
    }
  },
  {
    "_score": 9.911845,
    "_source": {
      "name": "Galley",
      "tags": [
        "php",
        "html",
        "docker",
        "website"
      ]
    }
  }
]
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 23, 2018, 2:34pm UTC](https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900/2 "2018-11-23T14:34:32Z")

</div>

What you're seeing is probably caused by the `tags` field being mapped as a `text` field. [`text` fields go through a process called text analysis](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html), which breaks up strings on things like whitespace and characters like `-`. This is why a query for "java" matches "java-8".

If you want to search for exact values, you will need to use [a `keyword` field](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) instead. If you have gone with the default mapping in Elasticsearch, there should already be a field called `tags.keyword` that will give you exactly what you need. You would query that `keyword` field like this:

```auto
GET localhost:9200/profile/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tags.keyword": "docker"
          }
        },
        {
          "term": {
            "tags.keyword": "php"
          }
        },
        {
          "term": {
            "tags.keyword": "java"
          }
        }
      ]
    }
  }
}

```

With regards to scoring, in this case you probably do not want to go with the [default BM25 scoring mechanism](https://www.elastic.co/blog/practical-bm25-part-1-how-shards-affect-relevance-scoring-in-elasticsearch). If you wrap your `term` queries in a `constant_score` query, the scoring will depend purely on the number of matching tags:

```auto
GET profile/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "filter": {
              "term": {
                "tags.keyword": "docker"
              }
            }
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "tags.keyword": "php"
              }
            }
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "tags.keyword": "java"
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![basvd](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@basvd](https://discuss.elastic.co/u/basvd)\
**Post date:** [November 24, 2018, 5:21pm UTC](https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900/3 "2018-11-24T17:21:01Z")

</div>

Thanks a lot for this clear explanation Abdon!

I'm going to change the mapping for the `tags` field and use `keyword` instead of `text` and I'm also going to use `constant_score` in the search query, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 22, 2018, 5:21pm UTC](https://discuss.elastic.co/t/exact-match-of-keywords-score-weight-keywords/157900/4 "2018-12-22T17:21:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
