Vartika
Vartika

Reputation: 1095

Stop Token Filter in java to use your wish of stop words

I want to add use stop words according to my need in the project of searching. As I am working on java I'll be needing java code. After a lot search I couldn't find code in java to add user defined stop words. I got this code. I tried to put in java code by using setting function but couldn't reached the result. Am I missing something. I want help to convert this code in java or some help just how to create a custom analyser of your wish?

PUT /my_index
 {
    "settings": {
    "analysis": {
        "filter": {
            "my_stop": {
                "type":       "stop",
                "stopwords": ["what", "where", "was"]
            }
        }
    }
  }
}

Upvotes: 0

Views: 459

Answers (1)

Marco Bonzanini
Marco Bonzanini

Reputation: 766

What is missing from the above configuration is that the stop-words should be defined in a custom analyzer (either by using a custom filter, or simply by defining a list), and then the analyzer must be applied to the desired field(s) via the mapping configuration.

To define your stop-words in a custom analyser:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": { 
          "type": "standard", 
          "stopwords": [ "what", "where", "was" ] 
        }
      }
    }
  }
}

After the analyzer is defined, you can use it in the mapping, e.g.

PUT /my_index/_mapping/my_type
{
    "properties": {
        "my_field": {
            "type":      "string",
            "analyzer":  "my_analyzer"
        }
    }
}

Upvotes: 1

Related Questions