# Elasticsearch Query for good title keyword results

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-for-good-title-keyword-results/191812>\
**Category:** Elasticsearch\
**Created:** [July 23, 2019, 12:39pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-good-title-keyword-results/191812 "2019-07-23T12:39:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonEritsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simoneritsch/32/50731_2.png) [@SimonEritsch](https://discuss.elastic.co/u/SimonEritsch)\
**Post date:** [July 23, 2019, 12:39pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-good-title-keyword-results/191812/1 "2019-07-23T12:39:00Z")

</div>

We have a elasticsearch index containing a catalog of products, that we want to search by title and description.

We want it to have the following constraints:

- We are searching title and description for occurences (matches in title should be twice as important as description)
- We want it to have a very light fuzzy search result (but still accurate results)
- Not matching results to the searchterm should not be filtered out, but only shown later (so matching results should be on top and worse results should be at the bottom)
- category\_id should filter products out (so no results of other categories should be shown)
- The created\_at attribute should be valued very high in sorting as well.  
products should lose score the "older" they get. (This is very important, because they lose importance with every day)

I have tried to create a query like that, but the results are really less than accurate. Sometimes finding completely unrelated stuff. I think that's because of the wildcard query.

Also I think there must be a more elegant solution for the "created\_at" scoring. Right?

I am using Elasticsearch 6.2

This is my current code. I would be happy to see an elegant solution for this:

```
    {
      "sort": [
        {
          "_score": {
            "order": "desc"
          }
        }
      ],
      "min_score": 0.3,
      "size": 12,
      "from": 0,
      "query": {
        "bool": {
          "filter": {
            "terms": {
              "category_id": [
                "212",
                "213"
              ]
            }
          },
          "should": [
            {
              "match": {
                "title_completion": {
                  "query": "Development",
                  "boost": 20
                }
              }
            },
            {
              "wildcard": {
                "title": {
                  "value": "*Development*",
                  "boost": 1
                }
              }
            },
            {
              "wildcard": {
                "title_completion": {
                  "value": "*Development*",
                  "boost": 10
                }
              }
            },
            {
              "match": {
                "title": {
                  "query": "Development",
                  "operator": "and",
                  "fuzziness": 1
                }
              }
            },
            {
              "range": {
                "created_at": {
                  "gte": 1563264817998,
                  "boost": 11
                }
              }
            },
            {
              "range": {
                "created_at": {
                  "gte": 1563264040398,
                  "boost": 4
                }
              }
            },
            {
              "range": {
                "created_at": {
                  "gte": 1563256264398,
                  "boost": 1
                }
              }
            }
          ]
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 23, 2019, 2:05pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-good-title-keyword-results/191812/2 "2019-07-23T14:05:02Z")

</div>

Hi Simon,

> [@SimonEritsch](#):
>
> I have tried to create a query like that, but the results are really less than accurate. Sometimes finding completely unrelated stuff. I think that's because of the wildcard query.

I think the unrelated stuff is because you made the range queries part of the same `should` clause as the text search - effectively asking for _"stuff that matches my search terms OR recent stuff"_.

You should put the text criteria in a `must` clause. Roughly speaking:

```
bool
    filter
        category selections
    must
        bool
           should
                 match field X with search term
                 match field Y with search term
    should
        recency boosts

```

---

<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:** [August 20, 2019, 2:05pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-good-title-keyword-results/191812/3 "2019-08-20T14:05:02Z")

</div>

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