Home > Mobile >  Settings to remove words from document is not working for elasticsearch DSL query
Settings to remove words from document is not working for elasticsearch DSL query

Time:06-20

My dictionary is below, I have loaded the cluster

[
{'id':1,  'name': 'weather report', 'description': 'Today is cooler weather'},
{'id':2,  'name': 'weather report', 'description': 'Today is hotter weather'},
{'id':3,  'name': 'weather report', 'description': 'Today is warm weather'}
]

I loaded the cluster

for i in wea:
    es.index(index="wea", body=i, id=i['id'])
  • I am searching for hot*
  • I have stop words hotter
  • so in the output there is no content

Below is the code

es.indices.close("wea")
settings = {
  "settings": {
    "analysis": {
      "analyzer": {
        "blogs_analyzer": {
          "type": "standard",
          "stopwords": [
            "hotter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "text",
        "analyzer": "blogs_analyzer"
      },
      "name": {
        "type": "text",
        "analyzer": "blogs_analyzer"
      },
      "description": {
        "type": "text",
        "analyzer": "blogs_analyzer"
      }
    }
  }
}
# es.indices.create(index="wea", ignore=400, body=settings)
es.indices.put_settings(index="wea", body=settings)
es.indices.open("wea")
  • The above scenario is perfectly working fine. now the problem

If i am changing my stopwords from 'hotter' to 'cooler' and i am searching cool* then my output has to be empty.

but I am getting {'id':3, 'name': 'weather report', 'description': 'Today is cooler weather'}

I found one solution i have to reload the cluster? Is that correct approach? any way to achieve without reloading

CodePudding user response:

Instead of reloading the cluster, you can close the index and use the update index settings API to update the analyzer configuration, then reopen the index.

You can use the below commands :

POST /<index-name>/_close

PUT /<index-name>/_settings
{
    "analysis": {
        "analyzer": {
            "blogs_analyzer": {
                "type": "standard",
                "stopwords": [
                    "hotter",
                    "cooler"
                ]
            }
        }
    }
}

POST /<index-name>/_open

The updated stopwords would only apply to new or modified documents, not to existing documents.

You must reindex the documents if you want to apply the modifications to the existing documents.

  • Related