Elasticsearch NEST 2.x字段名称

时间:2016-05-03 01:12:29

标签: couchbase nest elasticsearch-net elasticsearch-2.0

我正在升级到NEST 2.3.0并尝试重写最初为NEST 1.x编写的所有查询。 我正在使用Couchbase传输插件将数据从Couchbase推送到Elasticsearch。

POCO

 public class Park
    {
        public Park()
        {

        }

        public bool IsPublic { get; set; }
    }

映射就像这样

"mappings": {
"park": {
            "_source": {
                "includes": [
                  "doc.*"
                ],
                "excludes": [
                  "meta.*"
                ]
            },
            "properties": {
                "meta": {
                    "properties": {
                        "rev": {
                            "type": "string"
                        },
                      "flags": {
                        "type": "long"
                      },
                      "expiration": {
                        "type": "long"
                      },
                      "id": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                },
              "doc": {
                "properties": {

                  "isPublic": {
                    "type": "boolean"
                  }
                }
              }
            }
        }
      }

elasticsearch中的示例文档

    {
  "_index": "parkindex-local-01",
  "_type": "park",
  "_id": "park_GUID",
  "_source": {
    "meta": {
      "expiration": 0,
      "flags": 33554433,
      "id": "park_GUID",
      "rev": "1-1441a2c278100bc00000000002000001"
    },
    "doc": {
      "isPublic": true,
      "id": "park_GUID"
    }
  }
}

我在NEST的查询

 var termQuery = Query<Park>.Term(p => p.IsPublic, true);
        ISearchResponse<T> searchResponse = this.client.Search<T>(s => s.Index("parkindex-local-01")
                     .Take(size)
                     .Source(false)
                     .Query(q => termQuery));

此查询转到Elasticsearch,如下所示

{
  "size": 10,
  "_source": {
    "exclude": [
      "*"
    ]
  },
  "query": {
    "term": {
      "isPublic": {
        "value": "true"
      }
    }
  }
}

它不会检索任何数据,只有在我使用 “doc。” 作为字段名称前缀时它才有效。所以查询变为如下

{
  "size": 10,
  "_source": {
    "exclude": [
      "*"
    ]
  },
  "query": {
    "term": {
      "doc.isPublic": {
        "value": "true"
      }
    }
  }
}

如何在NEST中编写上面的查询,以便它可以正确解释字段名称,我尝试使用带有路径设置为“doc”的嵌套查询,但这给出了一个错误,说字段不是嵌套类型。

我是否需要更改映射?

这一切都曾经在Elasticsearch 1.x和NEST 1.x中运行,我想这与破坏对字段名称约束的更改有关。

1 个答案:

答案 0 :(得分:0)

Fields can no longer be referenced by shortnames in Elasticsearch 2.0.

isPublicdoc字段的属性,它被映射为object类型,因此通过属性的完整路径引用是正确的事情。

NEST 2.x has some ways to help with field inference,一个例子

public class Park
{
    public Doc Doc { get; set;}
}

public class Doc
{
    public bool IsPublic { get; set;}
}

var termQuery = Query<Park>.Term(p => p.Doc.IsPublic, true);

client.Search<Park>(s => s.Index("parkindex-local-01")
             .Take(10)
             .Source(false)
             .Query(q => termQuery));

结果

{
  "size": 10,
  "_source": {
    "exclude": [
      "*"
    ]
  },
  "query": {
    "term": {
      "doc.isPublic": {
        "value": true
      }
    }
  }
}

您可能还想查看automapping documentation