残念怪蜀黍 2020-01-31 20:08 采纳率: 0%
浏览 761

Elasticsearch nested 嵌套分词查询返回结果不准确

通过ES分词搜索文档的标题和内容,标题和内容这两个字段是分词的text字段。
因为一个文档时多语言的,所以mapping设置是嵌套的,但是搜索出来的结果并不是我期望的。

{
    "mappings": {
        "properties": {
            "title": {     // 标题字段
                "type": "nested",
                "properties": {
                    "lang": {   // 语种字段,"zh/en/ja..."
                        "type": "keyword"
                    },
                    "value": {   // 标题对应的值
                        "type": "text",
                        "search_analyzer": "ik_max_word",
                        "analyzer": "ik_max_word",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    }
                }
            },
            "content": {  // 正文内容
                "type": "nested",
                "properties": {
                    "lang": {
                        "type": "keyword"
                    },
                    "value": {
                        "type": "text",
                        "search_analyzer": "ik_max_word",
                        "analyzer": "ik_max_word",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    }
                }
            },
            "baseCode": { //属于某个知识库下
                "type": "keyword"
            },
            "knowledgeId": { // 唯一ID
                "type": "long"
            },
            "order": { // 序号
                "type": "integer"
            },
            "publish": { // 发布状态
                "type": "integer"
            },
            "status": { // 逻辑删除,0删除1存在
                "type": "integer"
            }
        }
    }
}

这个是查询条件:

{
    "query": {
        "bool": {
            "must": [{
                    "bool": {
                        "should": [{
                                "nested": {
                                    "query": {
                                        "bool": {
                                            "must": [{
                                                    "term": {
                                                        "title.lang": {
                                                            "value": "zh",
                                                            "boost": 1
                                                        }
                                                    }
                                                },
                                                {
                                                    "match": {
                                                        "title.value": {
                                                            "query": "测试",
                                                            "operator": "OR",
                                                            "prefix_length": 0,
                                                            "max_expansions": 50,
                                                            "fuzzy_transpositions": true,
                                                            "lenient": false,
                                                            "zero_terms_query": "NONE",
                                                            "auto_generate_synonyms_phrase_query": true,
                                                            "boost": 1.5
                                                        }
                                                    }
                                                }
                                            ],
                                            "adjust_pure_negative": true,
                                            "boost": 1.5
                                        }
                                    },
                                    "path": "title",
                                    "ignore_unmapped": false,
                                    "score_mode": "avg",
                                    "boost": 2
                                }
                            },
                            {
                                "nested": {
                                    "query": {
                                        "bool": {
                                            "must": [{
                                                    "term": {
                                                        "content.lang": {
                                                            "value": "zh",
                                                            "boost": 1
                                                        }
                                                    }
                                                },
                                                {
                                                    "match": {
                                                        "content.value": {
                                                            "query": "测试",
                                                            "operator": "OR",
                                                            "prefix_length": 0,
                                                            "max_expansions": 50,
                                                            "fuzzy_transpositions": true,
                                                            "lenient": false,
                                                            "zero_terms_query": "NONE",
                                                            "auto_generate_synonyms_phrase_query": true,
                                                            "boost": 1
                                                        }
                                                    }
                                                }
                                            ],
                                            "adjust_pure_negative": true,
                                            "boost": 1
                                        }
                                    },
                                    "path": "content",
                                    "ignore_unmapped": false,
                                    "score_mode": "avg",
                                    "boost": 1
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 2
                    }
                },
                {
                    "term": {
                        "status": {
                            "value": 1,
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "categoryType": {
                            "value": 0,
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "baseCode": {
                            "value": "B8rqalut2bajk",
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "publish": {
                            "value": 1,
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "isOpen": {
                            "value": 1,
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    }
}

期望结果是标题是“测试标题并有多个测试词语”的数据在第一个,因为标题和内容都匹配到了,并且次数最多。但是第一条返回的并不是这条。
图片说明

但实际返回的第一条数据是这个:
图片说明
怎么样才能返回第一条是我想要的数据呢?

  • 写回答

1条回答 默认 最新

  • zhangpan_soft 2023-12-05 14:19
    关注

    这个es的默认评分机制问题, 我记得should好像是不参与评分的, 我忘了, 你可以执行下explain, 看下查询计划, 会告诉你详细的评分计算, 就会知道是什么原因

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作