RavenDB:如何将查询转换为lucene查询?

时间:2015-12-23 22:02:12

标签: c# nested ravendb hierarchy

我有以下代码:

public class Continent
{
    public string Name { get; set; }
    public List<Country> Countries{ get; set; }
}

public class Countries
{
   public string Name { get; set; }
   public List<Province> Provinces{ get; set; }
}

public class Province
{
    public string Name { get; set; }
    public List<Province> Cities { get; set; }
}

public class City
{
    public string Name { get; set; }
    public string Address   { get; set; }
}

我想将以下查询转换为lucene查询(例如,Where,Any),以便我得到Aloma和Hemane在同一个省(而不是在不同的省份)的各大洲各自的地址123和435:

 var queryResults = from continent in session
                              .Advanced.DocumentQuery<Continent>()
                               from country in continent.Countries
                               from prov in country.Provinces
                               let cities_ = prov.Cities
                               let fi = cities_.Where(fp => fp.Name == "Aloma" && fp.Address == "123").FirstOrDefault()
                               let fj = cities_.Where(fk => fk.Name == "Hemane" && fk.Address == "435").FirstOrDefault()
                               where fi != null && fj != null
                               select continent;

我最初尝试了以下内容,但是当Aloma和Hemane在同一个省(我想要的)时它会返回结果,但是当Aloma和Hemane在不同的省份时(我不想要的):

var queryResultsLucene = session.Advanced.DocumentQuery<Continent>()
                                 .Where("Countries,Provinces,Cities,Name:Aloma")
                                 .AndAlso()
                                 .Where("Countries,Provinces,Cities,Address:123")
                                 .Intersect()
                                  .Where("Countries,Provinces,Cities,Name:Hemane")
                                 .AndAlso()
                                 .Where("Countries,Provinces,Cities,Address:435")
                                 .OfType<Topic>()
                                 .ToList();
你能帮帮我吗? 提前致谢

2 个答案:

答案 0 :(得分:0)

我会在RavenDB: how to transform a session.Query into a session.Advanced.DocumentQuery?上使用我的回复中的Lucene查询。我不会在很多城市使用它。

 var continentsToFindByCity = new List<City>(){new City{Name = "Aloma", Address = "123"}, new City{Name = "Hemane", Address = "435"}};
 var results = new List<Continent>();


    foreach(var city in continentsToFindByCity)
    {
        var tempResults = session.Advanced.DocumentQuery<Continent>().Where(
            string.Format("Countries,Provinces,Cities,Name: {0} AND Countries,Provinces,Cities,Address: {1}", city.Name, city.Address)).ToList();

        if(tempResults.Count > 0)
            results.AddRange(tempResults);
    }

答案 1 :(得分:0)

您可以使用以下查询:

var queryResults = session.Advanced.DocumentQuery<Continent>()
                    .OpenSubClause()
                        .WhereEquals("Countries,Provinces,Cities.Name", "Aloma")
                        .AndAlso()
                        .WhereEquals("Countries,Provinces,Cities.Address", "123")
                    .CloseSubClause()
                    .AndAlso()
                    .OpenSubClause()
                        .WhereEquals("Countries,Provinces,Cities.Name", "Hemane")
                        .AndAlso()
                        .WhereEquals("Countries,Provinces,Cities.Address", "Hemane")
                    .CloseSubClause()
                    .ToList();