索引更改后在elasticsearch中重新索引数据

问题描述 投票:0回答:1

我有一个字段

carName
,它使用一些分析器:

@Field(type = FieldType.Text, searchAnalyzer = "myAnalyzer", analyzer = "myAnalyzer")
private String carName;

myAnalyzer
分析器如下所示:

{
  "index": {
    "analysis": {
      "filter": {
        "myStopwords": {
          "ignore_case": "true",
          "type": "stop",
          "stopwords": [
            "word1",
            "word2"
          ]
        } 
      },
      "char_filter": {
        "myTrimmer": {
          "flags": "CASE_INSENSITIVE",
          "pattern": "(a\\.)\\s+(g\\.)|(e\\.)\\s+([g,k,v]\\.)|(c\\.)\\s+(o\\.)",
          "replacement": "$1$2$3$4$5$6",
          "type": "pattern_replace"
        } 
      },
      "analyzer": {
        "myAnalyzer": {
          "filter": [
            "lowercase",
            "unique",
            "myStopwords"
          ],
          "char_filter": [
            "myTrimmer"
          ],
          "type": "custom",
          "tokenizer": "whitespace"
        } 
      }
    }
  }
}

现在

myStopwords
将扩大或缩小。在我的数据库中,我有
CAR
实体,一旦有人添加新车,它就会在 ES 中作为文档进行索引。当有人更改停用词列表时我该怎么办?是否可以只在 ElasticSearch 端刷新数据,甚至不从我的数据库中读取数据?或者由于停用词列表的更改,
carName
所在索引中的某些数据可能会在索引期间丢失 - 例如,属于停用词列表的单词?在这种情况下,不幸的是,我需要再次从数据库中读取汽车并再次索引它们..?

据我了解

analyzer
并且在我的例子中
myAnalyzer
在 ES 的索引过程中使用,那么乍一看似乎如果我更改停用词列表(所以在这种情况下它是
analyzer
更改),那么我应该重新索引我的汽车,但也许我错了?如果一辆汽车被命名为“Ford King Taurus”并且
King
不在停用词列表中,那么如果我将
King
添加到停用词列表中会发生什么。如果“King”在停用词列表中并且某些文档已被索引现在已从该列表中删除,那么搜索会发生什么..在此类映射更改后搜索会正常工作吗?

我读到了有关 UpdateByQuery 方法的信息,我认为该方法可用于一些类似的情况来更新文档的部分内容。但可以用在这里吗?我的意思是..如果有必要,我如何告诉 Elasticsearch 由于停用词列表更改而刷新所有 carNames ?

elasticsearch spring-data-elasticsearch
1个回答
0
投票

如果您使用相同的分析器、索引时间和搜索时间,并且更新停用词列表,则索引时间和搜索时间分析器将立即使用新的停用词列表,但是,任何已经存在的内容索引不会更新,您需要

_update_by_query
您的索引才能应用新的停用词。

一个简单的例子:

如果您索引

Ford King Taurus
并且停用词列表不包含
King
,则以下标记将被索引:
Ford
King
Taurus
。在搜索时,您可以使用这三个术语中的任何一个来查找文档。

然后在停用词列表中添加

King
关闭并重新打开索引以刷新分析器。此时,之前带有
Ford King Taurus
的文档将无法再使用
King
进行搜索,因为搜索分析器现在会忽略
King
,即使标记
King
仍被编入索引。不过,您仍然可以使用
standard
搜索分析器并搜索
king
来查找文档,因为
king
标记仍已编入索引。

但是,如果您索引一个新文档,例如

Seat King
,那么只有
Seat
会被索引,并且搜索
King
将不会产生任何结果。

如果您希望以前的文档获取新的停用词

King
,您需要重新索引文档,或者简单地使用
_update_by_query
更新索引,以便源文档自行重新索引,但索引时间会缩短具有新停用词列表的分析器,包括
King

以下是上述所有解释的快速摘要:

# 1. You create your index like normal
PUT test2
{
   "settings": {...},
   "mappings": {...}
}

# 2. You index "Ford King Taurus"
POST test2/_doc/1 
{
  "carName": "Ford King Taurus"
}

# 3. You can find it searching for "king"
POST test2/_search 
{
  "query": {
    "match": {
      "carName": "king"
    }
  }
}

# 4. You close the index, add "king" a new stop words and reopen the index
POST test2/_close
PUT test2/_settings
{
  "index": {
    "analysis": {
      "filter": {
        "myStopwords": {
          "ignore_case": "true",
          "type": "stop",
          "stopwords": [
            "word1",
            "word2",
            "king"
          ]
        }
      },
      "analyzer": {
        "myAnalyzer": {
          "filter": [
            "lowercase",
            "unique",
            "myStopwords"
          ],
          "type": "custom",
          "tokenizer": "whitespace"
        }
      }
    }
  }
}
POST test2/_open

# 5. You cannot find the document searching for "king"
POST test2/_search
{
  "query": {
    "match": {
      "carName": {
        "query": "king"
      }
    }
  }
}
=> No results

# 6. But you can still find it using the standard search analyzer
POST test2/_search
{
  "query": {
    "match": {
      "carName": {
        "query": "king",
        "analyzer": "standard"
      }
    }
  }
}
=> 1 result

# 7. You update your index in place
POST test2/_update_by_query

# 8. None of the search queries will find anything with "king"
© www.soinside.com 2019 - 2024. All rights reserved.