我的弹性搜索索引(my_index
)中存储了以下文档:
{
"name": "111666"
},
{
"name": "111A666"
},
{
"name": "111B666"
}
我希望能够使用name
字段的确切值以及值的字符修剪版本来查询这些文档。
实施例
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "111666"
}
}
}
}
应该返回上面提到的所有(3)文件。
另一方面:
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "111a666"
}
}
}
}
应该只返回一个文档(与name
字段提供的值完全匹配的文档)。
我没有找到配置my_index
设置的方法来支持此类功能(自定义搜索/索引分析器等)。
我在这里要提一下,我正在使用ElasticSearch的Java API(QueryBuilders
)来实现上述查询,所以我想用Java方式来做。
逻辑
1) Check if the provided query-string contains a letter
2) If yes (e.g 111A666), then search for 111A666 using a standard search analyzer
3) If not (e.g 111666), then use a custom search analyzer that trims the characters of the `name` field
问题
1)是否可以通过某种方式配置数据在弹性搜索中的存储/索引方式来实现?
2)如果没有,是否可以在运行时有条件地更改字段的分析仪? (使用Java)
答案 0 :(得分:0)
您可以轻松地使用分析器或任何自定义分析器中的任何构建来在elasticsearch中映射文档。有关分析仪的更多信息是here
"term"
查询搜索完全匹配。您可以找到有关完全匹配的更多信息here (Finding Exact Values)
但是一旦创建了索引,就无法更改它。如果要更改任何索引,则必须创建新索引并将所有数据迁移到新索引。
答案 1 :(得分:0)
您的问题是关于分析器在索引和查询时的不同逻辑。
Q1的解决方案是在索引时生成两个令牌(111a666 - > [111a666,11166]),但仅在查询时生成令牌(111a666 - > 111a666和111666 - > 111666)。
I.m.h.o。你必须生成一个新的分析器 支持https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern_replace-tokenfilter.html的https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-capture-tokenfilter.html支持{{3}}。 或者您可以使用两个字段(一个包含原始字段,另一个没有字母)并搜索两个字段。