Stop phrases rather words

Hi, I am sure this would have been discussed earlier but not able to find much. We have a requirement to exclude phrase rather than individual word e.g. if name is 'Big Elephant', it should be excluded but it is either 'Big' or "Elephant' or includes other words, they must not be filtered. Is there a way to use phrases in the stopword file.

cheers,
Amit

1 Like

I think you can do that with combination of synonym filter and stop words.

DELETE test

PUT test
{
  "settings": {
    "analysis": {
      "filter": {
        "phrase_matcher": {
          "type": "synonym",
          "synonyms": [
            "big elephant, small giraffe => stop_me"
          ]
        },
        "my_stop": {
          "type": "stop",
          "stopwords": [
            "stop_me"
          ]
        }
      },
      "analyzer": {
        "stop_phrase": {
          "tokenizer": "standard",
          "fitler": [
            "lowercase",
            "phrase_matcher",
            "stop"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "text" : {
          "type": "text",
          "analyzer": "stop_phrase"
        }
      }
    }
  }
}

POST /test/doc/_bulk?refresh
{ "index" : { "_id" : "1" } }
{ "text" : "A big elephant and a very small giraffe" }
{ "index" : { "_id" : "1" } }
{ "text" : "A very small elephant and a big giraffe" }

GET /test/doc/_search
{
  "query": {
    "match": {
      "text": "elephant"
    }
  }
}

GET /test/doc/_search
{
  "query": {
    "match": {
      "text": "big elephant"
    }
  }
}

GET /test/doc/_search
{
  "query": {
    "match_phrase": {
      "text": "small elephant"
    }
  }
}
1 Like

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