弹性嵌套访问子 属性 文本类型

Elastic nest accessing sub property of text type

我创建了文本 属性 name 我还创建了 sub 属性 as words_count of name 并且我想对名称的 words_count 进行范围查询。我如何使用 Nest 在 C# 中访问它。

"mappings": {
"person": {
  "properties": {
    "name": { 
      "type": "text",
      "fields": {
        "keyword": { 
          "type": "keyword"
        },
        "words_count": { 
          "type": "token_count",
          "analyzer": "standard"
        },
        "length": { 
          "type": "token_count",
          "analyzer": "character_analyzer"
        }
      }
    }
  }
}
}

我有名称的长度,但它来自 c# 字符串长度。我想访问在 elastic 中创建的名称的 words_count sub 属性。

c# code

Func<QueryContainerDescriptor<MyType>, QueryContainer> query = m => m
                                    .Range(r => r.Field(f => f.name.words_count).Relation(RangeRelation.Within)
                                    .GreaterThanOrEquals(10).LessThanOrEquals(14));

我想用 elastic nest 替换 f.name.words_count。我是否需要为具有 属性 长度的名称创建 class。

您无需创建 POCO 属性 即可映射到 multi-field(通常也称为 fieldssub-fields)。

它们是能够以多种不同方式索引单个输入的功能,这在搜索用例中很常见。例如,使用多种不同类型的分析对街道地址进行索引。

您可以使用 .Suffix(...) 扩展方法来引用 multi-field

Func<QueryContainerDescriptor<MyType>, QueryContainer> query = m => m
    .Range(r => r
        .Field(f => f.name.Suffix("words_count"))
        .Relation(RangeRelation.Within)
        .GreaterThanOrEquals(10)
        .LessThanOrEquals(14)
    );