通过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
}
}
}
期望结果是标题是“测试标题并有多个测试词语”的数据在第一个,因为标题和内容都匹配到了,并且次数最多。但是第一条返回的并不是这条。
但实际返回的第一条数据是这个:
怎么样才能返回第一条是我想要的数据呢?