我正在使用Elasticsearch 7.6并使用以下映射定义索引关键字列表:
"keywords" : {
"type" : "text",
"analyzer": "std_folded",
"store": true,
"fields": {
"keyword": {
"type": "keyword"
}
}
},
如您所见,该字段的名称为keywords
,将给出一个单词列表。另外,我将keywords.keyword
定义为keyword
类型。
是否可以为每个单词分配一些值(分数)?我希望以后在脚本查询中检索该值。
答案 0 :(得分:1)
因此,如果不是每个单词都为每个字段分配一个特定的值(如您的情况),那么您可能使用了与index-time boost类似的东西,而5.0弃用了它。
据我了解,您的keywords
字段可以有多个值,例如foo
,bar
和baz
属于同一文档。并且您想给出foo:1
,bar:2
和baz:3
之类的所有不同分数,这是分数或提高场上价值,而不是场上价值。
{
"mappings": {
"properties": {
"keywords": {
"type": "nested" --> note this
}
}
}
}
{
"keywords" : [
{
"keyword" : "foo",
"score" : 1
},
{
"keyword" : "bar",
"score" : 2
},
{
"keyword" : "baz",
"score" : 3
}
]
}
foo
且得分为1
的文档{
"query": {
"nested": {
"path": "keywords",
"query": {
"bool": {
"must": [
{
"match": {
"keywords.keyword": "foo"
}
},
{
"match": {
"keywords.score": 1
}
}
]
}
},
"inner_hits": { --> notice this, it would bring inner doc `foo:1`
}
}
}
}
"hits": [
{
"_index": "nested",
"_type": "_doc",
"_id": "1",
"_score": 1.9808291,
"_source": {
"keywords": [
{
"keyword": "foo",
"score": 1
},
{
"keyword": "bar",
"score": 2
},
{
"keyword": "baz",
"score": 3
}
]
},
"inner_hits": {
"keywords": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.9808291,
"hits": [
{
"_index": "nested",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "keywords",
"offset": 0
},
"_score": 1.9808291, --> your expected result in inner hits
"_source": {
"keyword": "foo",
"score": 1
}
}
]