弹性同义词使用
情况:弹性同义词使用
使用弹性版本:2.3.1
我已经配置像这样
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"british,english",
"queen,monarch"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}
,这是伟大的弹性指数,当我查询文件和使用查询字词“英文”或“女王”我得到所有文件匹配英国和君主。当我在过滤器聚合中使用同义词时,它不起作用。例如
在我的索引我有5个文件,其中3个有君主,其中2个有女王
POST /my_index/_search
{
"size": 0,
"query" : {
"match" : {
"status.synonym":{
"query": "queen",
"operator": "and"
}
}
},
"aggs" : {
"status_terms" : {
"terms" : { "field" : "status.synonym" }
},
"monarch_filter" : {
"filter" : { "term": { "status.synonym": "monarch" } }
}
},
"explain" : 0
}
结果产生:
总共点击:
- 5 doc count(如预期的那样,太棒了!)
- 状态条件:5个文件数为女王(如预期的那样,太棒了!)
- 君主滤波器:0 DOC计数
我已经尝试了不同的同义词滤波器配置:
- 王后,辟
- 王后,辟=>王后
- 王后,辟=>女王,君主
但是,上述并没有改变结果。我想得出结论,也许你可以在查询时使用过滤器,但如果术语聚合工作,为什么不应该过滤,因此我认为它的同义词过滤器配置是错误的。更广泛的同义词过滤器示例可以在here找到。
问题:
如何使用/配置过滤器聚集同义词?
实施例中复制的情况下,上述: 1.创建和配置索引:
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"wlh,wellhead=>wellwell"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}
PUT my_index/_mapping/job
{
"properties": {
"title":{
"type": "string",
"analyzer": "my_synonyms"
}
}
}
2.Put两个文件:
PUT my_index/job/1
{
"title":"wellhead smth else"
}
PUT my_index/job/2
{
"title":"wlh other stuff"
}
3。执行wlh的搜索,它应该返回2个文档;有哪些不应该有0计数方面聚集了应有的2个文件wellwell和过滤器:
POST my_index/_search
{
"size": 0,
"query" : {
"match" : {
"title":{
"query": "wlh",
"operator": "and"
}
}
},
"aggs" : {
"wlhAggs" : {
"terms" : { "field" : "title" }
},
"wlhFilter" : {
"filter" : { "term": { "title": "wlh" } }
}
},
"explain" : 0
}
此查询的结果是:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"wlhAggs": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "wellwell",
"doc_count": 2
},
{
"key": "else",
"doc_count": 1
},
{
"key": "other",
"doc_count": 1
},
{
"key": "smth",
"doc_count": 1
},
{
"key": "stuff",
"doc_count": 1
}
]
},
"wlhFilter": {
"doc_count": 0
}
}
}
并且那是我的问题, wlhFilter应该至少有1个文档数。
所以是我的解决方案:
- 我创建了一个单独的领域,我用同义词分析仪上,因为 不是具有属性域(主场。属性)。
- 而最重要的问题是我的同义词被缩小了!例如,我有 英国英语=>英国。将其更改为 british,english,uk解决了我的问题,并且过滤器聚合为 返回了正确数量的文档。
希望这可以帮助某人,或者至少指向正确的方向。
编辑: 哦,上帝赞美文件!我完全解决了我的问题,使用过滤器(S!)聚合(链接here)。在过滤器配置中,我指定了Match类型的查询并且它工作正常!结束了这样的事情:
"aggs" : {
"messages" : {
"filters" : {
"filters" : {
"status" : { "match" : { "cats.saurus" : "monarch" }},
"country" : { "match" : { "cats.saurus" : "british" }}
}
}
}
}
我时间很短,所以如果需要的话,我可以在今天/明天晚些时候详细阐述一下。但下面应该工作:
DELETE /my_index
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"british,english",
"queen,monarch"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"title": {
"type": "text",
"analyzer": "my_synonyms",
"fielddata": true
}
}
}
}
}
POST my_index/test/1
{
"title" : "the british monarch"
}
GET my_index/_search
{
"query": {
"match": {
"title": "queen"
}
}
}
GET my_index/_search
{
"query": {
"match": {
"title": "queen"
}
},
"aggs": {
"queen_filter": {
"filter": {
"term": {
"title": "queen"
}
}
},
"monarch_filter": {
"filter": {
"term": {
"title": "monarch"
}
}
}
}
}
你能分享您为status.synonym
字段定义的映射?
编辑:V2
之所以你的过滤器的输出为0,是因为在Elasticsearch过滤器永远不会通过分析阶段。这意味着完全匹配。
聚合中的标记'wlh'不会被翻译为'wellwell',这意味着它不会出现在倒排索引中。这是因为在索引期间,你的'wlh'被翻译成'wellwell'。 为了达到您想要的效果,您必须将数据编入一个单独的字段并相应地调整您的过滤器。
你可以尝试这样的:
DELETE my_index
PUT /my_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"wlh,wellhead=>wellwell"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"job": {
"properties": {
"title": {
"type": "string",
"fields": {
"synonym": {
"type": "string",
"analyzer": "my_synonyms"
}
}
}
}
}
}
}
PUT my_index/job/1
{
"title":"wellhead smth else"
}
PUT my_index/job/2
{
"title":"wlh other stuff"
}
POST my_index/_search
{
"size": 0,
"query": {
"match": {
"title.synonym": {
"query": "wlh",
"operator": "and"
}
}
},
"aggs": {
"wlhAggs": {
"terms": {
"field": "title.synonym"
}
},
"wlhFilter": {
"filter": {
"term": {
"title": "wlh"
}
}
}
}
}
输出:
{
"aggregations": {
"wlhAggs": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "wellwell",
"doc_count": 2
},
{
"key": "else",
"doc_count": 1
},
{
"key": "other",
"doc_count": 1
},
{
"key": "smth",
"doc_count": 1
},
{
"key": "stuff",
"doc_count": 1
}
]
},
"wlhFilter": {
"doc_count": 1
}
}
}
希望这有助于!
我猜关键是“fielddata”:true。如果是这种情况,Id有兴趣知道为什么同义词领域术语聚合工作没有设置和过滤一个doesnt。需要了解如何使用NEST设置字段数据选项,因为据我所见,没有直接的方法来设置真正的标志。谢谢你的收获,会回来给你 –
如果你能向我展示你的同义词领域的映射,我们可以看到发生了什么问题。看起来你正在使用的过滤器聚合可能是针对未分析的字段。如果这是真的,那么ES不会将“女王”存储在倒排索引中,因为它不使用同义词分析器进行分析。 我有fielddata = true的原因是因为(没有设置)不可能在'文本'字段上聚合。这就是为什么你似乎在不解决同义词的字段上进行聚合:) –
我已经添加了一个示例代码部分,您可以使用它来复制我的问题。我应该真的开始,让问题更清洁,但不要混淆东西,我不会编辑它的问题部分。此外,ES版本是2.3.1,我无法复制您的示例,因为文本不是属性/功能,直到更高版本。当然,如果需要进行版本升级才能使其发挥作用,那么这不是问题,但是如果这是“最后的手段”,Id更喜欢。 –
很好,你明白了! –
我在同一时间发布更新。甚至找到了更好的解决方案。谢谢你的帮助 –