【问题标题】:Elasticsearch NEST 2.x Field NamesElasticsearch NEST 2.x 字段名称
【发布时间】:2016-05-03 01:12:28
【问题描述】:

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

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 中工作,我想这与对字段名称约束的破坏性更改有关。

【问题讨论】:

    标签: couchbase nest elasticsearch-net elasticsearch-2.0


    【解决方案1】:

    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

    【讨论】:

    • 感谢您的回复,我的POCO类型没有Doc对象,IsPublic是Park类的直接属性。 Elasticsearch 中的“doc”在那里,因为我使用 couchbase 传输插件将文档从 Couchbase 移动到 Elasticsearch,我的代码将文档添加到 Couchbase,然后传输插件将文档从 Couchbase 移动到 Elasticsearch。我从不直接在 Elastcsearch 中建立索引。我认为字段推断是我所需要的,但是如何在上面的查询中使用它?
    • park 映射有两个 object 类型字段,docmeta - doc 属性将存储在 _source 文档中,但 meta 不会因为_source 包含/排除。 meta 字段仍然可以搜索。在 Elasticsearch 2 之前,您可以使用 IsPublic,就像它是直接在 Park 类型上的属性一样,但在 Elasticsearch 2 中,您需要使用字段的完整路径 doc.isPublic。您可以更改您的 CLR 类型以适应我上面所说的。
    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2015-06-11
    相关资源
    最近更新 更多