coexsystem
coexsystem

Reputation: 25

Elasticsearch associating exact match terms

I have a search index of filenames containing over 100,000 entries that share about 500 unique variations of the main filename field. I have recently made some modifications to certain filename values that are being generated from my data. I was wondering if there is a way to link certain queries to return an exact match. In the following query:

"query": {
  "bool": {
    "must": [
      {
        "match": {
          "filename": "foo-bar"
        }
      }
    ],
  }
}

how would it be possible to modify the index and associate the results so that above query will also match results foo-bar-baz, but not foo-bar-foo or any other variation?

Thanks in advance for your help

Upvotes: 0

Views: 121

Answers (2)

Bhavya
Bhavya

Reputation: 16192

Adding a working example with index data and search query. (Using the default mapping)

Index Data:

{
    "fileName": "foo-bar"
}
{
    "fileName": "foo-bar-baz"
}
{
    "fileName": "foo-bar-foo"
}

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "fileName.keyword": "foo-bar" 
          }
        },
        {
          "match": {
            "fileName.keyword": "foo-bar-baz" 
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.9808291,
                "_source": {
                    "fileName": "foo-bar"
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.9808291,
                "_source": {
                    "fileName": "foo-bar-baz"
                }
            }
        ]

Upvotes: 0

Jaycreation
Jaycreation

Reputation: 2089

You can use a term query instead of a match query. Perfect to use on a keyword: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

Upvotes: 1

Related Questions