【问题标题】:Elasticsearch.Net query using NEST and nested objects to retrieve documentsElasticsearch.Net 查询使用 NEST 和嵌套对象来检索文档
【发布时间】:2017-01-30 20:08:21
【问题描述】:

我在应用程序中使用 Elasticsearch.Net 和 NEST,并且在基于嵌套对象 ID 进行搜索时无法访问 Elasticsearh 索引中的文档。数据结构为 invoice -> lineItems -> rowItems。我想根据这些 rowItems Id 进行搜索。索引的(简化)映射为:

"invoice": {
    "properties": {
      "lineItems": {
        "properties": {
          "accountId": {
            "index": "not_analyzed",
            "type": "string"
          },
          "listItems": {
            "properties": {
              "itemName": {
                "analyzer": "str_index_analyzer",
                "term_vector": "with_positions_offsets",
                "type": "string",
                "fields": {
                  "raw": {
                    "analyzer": "str_search_analyzer",
                    "type": "string"
                  }
                }
              },
              "listItemID": {
                "index": "not_analyzed",
                "type": "string"
              }
            }
          }
        }
      }
    }
}

当我在其中一个嵌套对象的 chrome 中进行感觉搜索时,我可以成功检索它:

POST /_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
            "lineItems.rowItems.rowItemID" : "23f2157f-eb21-400d-b3a1-a61cf1451262"            
        }} 
      ]
    }
  }
}

返回文档类型发票及其所有详细信息。

我一直在尝试使用 NEST 执行此操作的代码,但到目前为止都失败了。我有一个 rowItemIds 列表,我想获取与这些 ID 完全匹配的所有发票文档。这是我目前拥有的:

            var result = Execute(client => client.Search<Invoice>(s => s
    .Aggregations(a => a
        .Nested("my_nested_agg", n => n
            .Path("lineItems")
            .Aggregations(aa => aa
                .Filter("my_avg_agg", avg => avg
                    .Field(p => searchIds.Contains(p.LineItems.First().RowItems.First().TrackingItemID))
                )
            )
        )
    )
));

其中 searchIds 是我正在搜索的 rowItemIds 列表。上面的代码是完全错误的,我不熟悉如何做到这一点的语法。任何帮助将不胜感激。

【问题讨论】:

  • 我在这里注意到:stackoverflow.com/questions/29346935/… 进行嵌套搜索需要嵌套关键字,例如:“type”:“nested” 是否可以在没有这个的情况下搜索这些 Id(我没有有)。此外,由于系统存在延迟,目前无法选择整个重新索引。

标签: c# nested nest elasticsearch-net


【解决方案1】:

在您希望跨 object 属性的属性进行查询的场景中需要嵌套类型。以你的例子,

  1. 如果您只想在lineItems 上查询listItems 的listItemID,那么使用object type 就可以了。

  2. 如果要查询lineItems 上的listItems 的listItemID 和 itemName,则需要将listItems 映射为nested type.

原因是在不使用nested 类型的情况下,特定listItem 的属性之间的关联在索引时不会被存储。对于nested 类型,关联被存储(nested 类型在内部存储为文档)。

您在 NEST 中的搜索查询非常相似;在这种情况下,match 查询不需要包含在 bool 查询 should 子句中

var client = new ElasticClient();

var searchResponse = client.Search<Invoice>(s => s
    .AllIndices()
    .AllTypes()
    .Query(q => q
        .Match(m => m
            .Field(f => f.LineItems.First().ListItems.First().ListItemID)
            .Query("23f2157f-eb21-400d-b3a1-a61cf1451262")
        )
    )
);

获取字段名的 lambda 表达式就是这样; 获取字段名称的表达式。

这会生成查询

POST http://localhost:9200/_search
{
  "query": {
    "match": {
      "lineItems.listItems.listItemID": {
        "query": "23f2157f-eb21-400d-b3a1-a61cf1451262"
      }
    }
  }
}

因为listItemID 是一个not_analyzed 字符串字段,您可以在此处使用term 查询,因为您可能不需要计算分数(在这种情况下,匹配是true 或 false),您可以将其包装在 bool 查询 filter 子句中,该子句可以利用过滤器缓存并且性能应该稍好一些。

要获取与 id 集合匹配的文档,我们可以使用 terms 查询

var ids = new[] {
    "23f2157f-eb21-400d-b3a1-a61cf1451262",
    "23f2157f-eb21-400d-b3a1-a61cf1451263",
    "23f2157f-eb21-400d-b3a1-a61cf1451264"
};

var searchResponse = client.Search<Invoice>(s => s
    .AllIndices()
    .AllTypes()
    .Query(q => q
        .Terms(m => m
            .Field(f => f.LineItems.First().ListItems.First().ListItemID)
            .Terms(ids)
        )
    )
);

最后,使用一元 + 运算符将其包装在 bool 查询 filter 子句中的简写

var searchResponse = client.Search<Invoice>(s => s
    .AllIndices()
    .AllTypes()
    .Query(q => +q
        .Terms(m => m
            .Field(f => f.LineItems.First().ListItems.First().ListItemID)
            .Terms(ids)
        )
    )
);

【讨论】:

  • 太棒了,感谢 Russ 解决了我对嵌套/对象的困惑。我实现了您的代码,并且可以成功检索硬编码的 Id。最后一部分:如果我有一个 Id 数组,我该如何修改上面的内容,以便它搜索数组中的所有 Id 而不是一个。
  • 太棒了,它有效。感谢您对这个全面且描述性的答案的帮助!
  • @CorribView 不用担心 :) 我为bool 查询filter 子句添加了关于速记的最后一段:elastic.co/guide/en/elasticsearch/client/net-api/2.x/…
  • 快速跟进问题:ES 查询似乎返回了 10 个文档的默认值,即使索引中有更多文档。如何显式设置它以返回所有匹配的文档?谢谢。
  • 您通常不希望在一个响应中返回所有个匹配的文档;您可以使用.Size(int)(别名为.Take(int))更改返回的文档数。您可以获取大小为 x 的文档页面,使用 .From(x)(别名为 .Skip(x))跳过以前的页面。如果你想做深度分页,看search_after,如果你想高效返回很多个文档,看scroll API
猜你喜欢
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多