# Partial matching against dates

**URL:** <https://discuss.elastic.co/t/partial-matching-against-dates/294190>\
**Category:** Elasticsearch\
**Created:** [January 12, 2022, 4:46pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190 "2022-01-12T16:46:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 12, 2022, 4:46pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/1 "2022-01-12T16:46:07Z")

</div>

I'm sure this is is easy but couldn't find a simple answer.

If I have 'XXXX2000' in an index and I search '01022000' how can I accept that as a partial match, whereby the more of the word that matches the higher the score.

For example there could also be 01XX2000 stored, so here the match would be higher.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 12, 2022, 5:24pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/2 "2022-01-12T17:24:29Z")

</div>

If you use ngrams to analyze your text, you will produce a lot of sub tokens. The more sub tokens match, the higher the score will be.

My 2 cents.

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 12, 2022, 5:25pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/3 "2022-01-12T17:25:58Z")

</div>

ngrams ... googling!

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 12, 2022, 5:45pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/4 "2022-01-12T17:45:54Z")

</div>

Hi David,

Quick eyes over, does this look correct (ngram from 1-8)?

```auto
      "settings": {
        "index": {
          "max_ngram_diff": 50
        },
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "filter": [
              ],
              "type": "custom",
              "tokenizer": "my_tokenizer"
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "token_chars": [
                "letter",
                "digit"
              ],
              "min_gram": 1,
              "type": "ngram",
              "max_gram": 8
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "dob": {
            "type": "text",
            "analyzer": "my_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }

```

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 12, 2022, 5:56pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/5 "2022-01-12T17:56:22Z")

</div>

Tested and is working. Thank you!

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 15, 2022, 5:40pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/6 "2022-01-15T17:40:19Z")

</div>

Hello again,

Requirements change means I need to revisit this. The date in question I can split onto the three indexes or combine. With this in mind, what is the best way to do:

DOB\_full = "10102000" or DOB\_YearMonth = "102000" or DOB\_Year = "2000"

and for a bonus point a means to weigh/boost them differently.

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 15, 2022, 6:39pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/7 "2022-01-15T18:39:33Z")

</div>

One more, this is along the lines of what I'm after, however the syntax is incorrect with

```auto
reason":"[match] unknown token [START_OBJECT] after [match_all]

```

```auto
should.push({
  match: {
    query: {
      match_all: {}
    },
    functions: [
      {
        filter: {
          match: { fields:['dob1'], query }
        },
        weight: 60
      },
      {
        filter: {
          simple_query_string: { fields:['dob1'], query: '**'+query.slice(2) }
        },
        weight: 40
      },
      {
        filter: {
          simple_query_string: { fields:['dob1'], query: ' ****'+query.slice(4) }
        },
        weight: 20
      }
    ],
    score_mode: "max",
  }
});

```

---

<div class="post-metadata">

**Author:** ![snoopy20](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@snoopy20](https://discuss.elastic.co/u/snoopy20)\
**Post date:** [January 15, 2022, 7:45pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/8 "2022-01-15T19:45:03Z")

</div>

Sorted I think.

```auto
function_score: {
  query: { match_all: {} },
  functions: [
    {
      filter: {
        match: {
          dob1: query
        }
      },
      weight: 1.1
    },
    {
      filter: {
        simple_query_string: {
          fields: ['dob1'],
          query: '**'+query.slice(2)
        }
      },
      weight: 1
    },
    {
      filter: {
        simple_query_string: {
          fields: ['dob1'],
          query: ' ****'+query.slice(4)
        }
      },
      weight: 0.85
    }
  ],
  score_mode: "max",
}

```

---

<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:** [February 12, 2022, 7:45pm UTC](https://discuss.elastic.co/t/partial-matching-against-dates/294190/9 "2022-02-12T19:45:21Z")

</div>

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