RavenDB 查询 returns 空
RavenDB query returns null
我正在使用 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。
在数据库记录器中,我可以看到传入的请求:
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,不必为对象的所有字段编制索引即可完整检索它,对吧?换句话说,我只需要索引字段来查找对象,而不是我要检索的所有字段;至少那是我的理解...
谢谢!
问题与不正确的大小写有关。
例如:
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();
我正在使用 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。
在数据库记录器中,我可以看到传入的请求:
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,不必为对象的所有字段编制索引即可完整检索它,对吧?换句话说,我只需要索引字段来查找对象,而不是我要检索的所有字段;至少那是我的理解...
谢谢!
问题与不正确的大小写有关。
例如:
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();