【问题标题】:RavenDB query returns nullRavenDB 查询返回 null
【发布时间】:2018-02-05 17:17:03
【问题描述】:

我使用的是 RavenDB 3.5.35183。我有一个类型:

import com.mysema.query.annotations.QueryEntity;

@QueryEntity
public class CountryLayerCount
{
    public String countryName;
    public int layerCount;
}

以及以下查询:

private int getCountryLayerCount(String countryName, IDocumentSession currentSession)
{           
    QCountryLayerCount countryLayerCountSurrogate = QCountryLayerCount.countryLayerCount;
    IRavenQueryable<CountryLayerCount> levelDepthQuery = currentSession.query(CountryLayerCount.class, "CountryLayerCount/ByName").where(countryLayerCountSurrogate.countryName.eq(countryName));
    CountryLayerCount countryLayerCount = new CountryLayerCount();
    try (CloseableIterator<StreamResult<CountryLayerCount>> results = currentSession.advanced().stream(levelDepthQuery))
    {
        while(results.hasNext())
        {
            StreamResult<CountryLayerCount> srclc = results.next();
            System.out.println(srclc.getKey());
            CountryLayerCount clc = srclc.getDocument();
            countryLayerCount = clc;
            break;
        }
    }
    catch(Exception e)
    {
    }
    return countryLayerCount.layerCount;
}

查询成功执行,并显示我正在检索的文档的正确 ID(例如“CountryLayerCount/123”),但其数据成员均为空。 where 子句也可以正常工作,国家名称用于检索各个国家/地区。这很简单,但我看不出我哪里出错了。 StreamResult 包含正确的键,但 getDocument() 不起作用 - 或者更确切地说,它不包含对象。该集合具有字符串 ID。

在 db 记录器中,我可以看到进来的请求:

Receive Request #  29: GET     - geodata - http://localhost:8888/databases/geodata/streams/query/CountryLayerCount/ByName?&query=CountryName:Germany
Request #  29: GET     -    22 ms - geodata    - 200 - http://localhost:8888/databases/geodata/streams/query/CountryLayerCount/ByName?&query=CountryName:Germany

当插入浏览器时,它会正确地给我:

{"Results":[{"countryName":"Germany","layerCount":5,"@metadata":{"Raven-Entity-Name":"CountryLayerCounts","Raven-Clr-Type":"DbUtilityFunctions.CountryLayerCount, DbUtilityFunctions","@id":"CountryLayerCounts/212","Temp-Index-Score":0.0,"Last-Modified":"2018-02-03T09:41:36.3165473Z","Raven-Last-Modified":"2018-02-03T09:41:36.3165473","@etag":"01000000-0000-008B-0000-0000000000D7","SerializedSizeOnDisk":164}}
]}

索引定义:

from country in docs.CountryLayerCounts
select new {
    CountryName = country.countryName
} 

AFAIK,不必索引对象的所有字段即可完整检索它,对吗?换句话说,我只需要索引字段来查找对象,而不是我想要检索的所有字段;至少这是我的理解……

谢谢!

【问题讨论】:

  • CountryLayerCount/ByName 长什么样子?
  • @Ayende:更新了问题

标签: java ravendb


【解决方案1】:

问题与不正确的大小写有关。

例如:

try (IDocumentSession sesion = store.openSession()) {
    CountryLayerCount c1 = new CountryLayerCount();
    c1.layerCount = 5;
    c1.countryName = "Germany";

    sesion.store(c1);
    sesion.saveChanges();
}

另存为:

{
    "LayerCount": 5,
    "CountryName": "Germany"
}

请注意,我们在 json 中使用大写字母作为属性名称(这仅适用于 3.X 版本)。

因此,为了使其正常工作,请更新 json 属性名称 + 编辑您的索引

from country in docs.CountryLayerCounts
select new {
    CountryName = country.CountryName
} 

顺便说一句。如果您有每个国家/地区的聚合,那么您可以简单地使用以下方式进行查询:

QCountryLayerCount countryLayerCountSurrogate = 
QCountryLayerCount.countryLayerCount;
    CountryLayerCount levelDepthQuery = currentSession
            .query(CountryLayerCount.class, "CountryLayerCount/ByName")
            .where(countryLayerCountSurrogate.countryName.eq(countryName))
            .single();

【讨论】:

  • 感谢您提供详细但简洁且非常有用的答案。罪魁祸首可能是我没有正确阅读文档并依赖假设...
猜你喜欢
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 2019-06-14
  • 2020-01-07
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多