FieldIndexOption未应用于嵌套对象

时间:2016-05-24 21:53:36

标签: elasticsearch nest

我使用ElasticSearch和NEST版本2来索引数据。我的数据对象可以具有相同类型的子对象。我正在使用注释来表示不分析某些字段。发生的事情是这个注释被应用于父对象,而不是子对象。我试图弄清楚如何修改我的注释以包含子实例。

我有这样的事情:

public class Person {
    public int Id {get; set;}

    [String(Index = FieldIndexOption.NotAnalyzed)]
    public string Code {get; set;}

    public Person child {get; set;}
}

当我第一次创建索引时如下:

client.Map<Person>(d => d.AutoMap());

映射如下所示:

"people": {
    "mappings": {
        "person": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "code": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "child": {
                    "type": "object"
                }
            }
        }
    }
}

我索引一些文件后如下:

client.Index(person);

映射更改为:

"people": {
    "mappings": {
        "person": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "code": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "child": {
                    "properties": {
                        "id": {
                            "type": "integer"
                        },
                        "code": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

说我有这样的文件:

{
    "id": 100,
    "code": "ABC100",
    "child": {
        "id": 123,
        "code": "ABC123"
    }
}

顶级人员的代码字段未分析,这很好,所以我可以这样搜索:

GET people/_search
{
  "query": {
    "term": {
      "code": "ABC100"
    }
  }
}

但是使用默认分析器分析孩子的代码字段,因此ABC123变为abc123。

因此,所有这些都会找到我的文件:

GET people/_search
{
  "query": {
    "term": {
      "child.id": 123
    }
  }
}
GET people/_search
{
  "query": {
    "term": {
      "child.code": "abc123"
    }
  }
}
GET people/_search
{
  "query": {
    "match": {
      "child.id": "ABC123"
    }
  }
}

但这不是:

GET people/_search
{
  "query": {
    "term": {
      "child.code": "ABC123"
    }
  }
}

我需要对对象注释进行哪些更改才能将相同的字段选项应用于子人? (顺便说一句,在现实生活中,我有几个未分析的领域,以及几个深度级别。)

1 个答案:

答案 0 :(得分:1)

Automapping in NEST does not recurse further than the top level by default,因此Receiver上的子属性被映射为对象,动态映射和字段类型推断在索引文档时在映射中创建AndroidManifest.xml字段。

NEST可以通过将深度参数传递给Person

来递归地映射类型
child

将导致以下映射

.AutoMap(int)

我们现在使用预期的字段映射设置将client.Map<Person>(m => m.AutoMap(1)); { "properties": { "id": { "type": "integer" }, "code": { "type": "string", "index": "not_analyzed" }, "child": { "properties": { "id": { "type": "integer" }, "code": { "type": "string", "index": "not_analyzed" }, "child": { "properties": {}, "type": "object" } }, "type": "object" } } } 映射到子对象上。我们还在id上获得code字段,映射为child

如果你知道深度只会是1,那么如果我们不希望顶级child上的object字段使用流利,我们可以略微整理映射映射

child

产生

child