简体   繁体   中英

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

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

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"
                }
            }
        ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM