0

我无法让我的 RavenDB 查询按预期工作。

我正在尝试根据用户搜索查询进行精确匹配,以匹配字符串中的所有单词。

因此,当我输入“Volkswagen”时,我想获得所有类型的“Volkswagen”汽车。如果我输入“Volkswagen Golf”,我只想得到“Volkswagen Golf”,依此类推。

到目前为止我所拥有的

public IEnumerable<Cars> GetSearch(string query)
{
    return _documentSession.Query<Car>("CarIndex").Search(x => x.SearchQuery, query);
}

//Also tried with this one
public IEnumerable<Cars> GetSearch(string query)
{
    return _documentSession.Query<Car>("CarIndex")
        .Search(x => x.Make, query)
        .Search(x => x.Model, query);
}


public class CarIndex : AbstractIndexCreationTask<Car>
{
    public CarIndex()
    {
        Map = cars => from car in cars select 
                new
                   {
                        car.SearchQuery, 
                        car.Make, 
                        car.Model, 
                        car.Year
                   };

        Index(x => x.Id, FieldIndexing.No);
        Index(x => x.SearchQuery, FieldIndexing.Analyzed);
        Index(x => x.Make, FieldIndexing.NotAnalyzed);
        Index(x => x.Model, FieldIndexing.NotAnalyzed);
        Index(x => x.Year, FieldIndexing.NotAnalyzed);
    }
}


private void AddDummyData()
{
    var cars = new List<Car>
    {
        new Car {Id = 1, Make = "Volkswagen", Model = "Touran", Year = 1998 },
        new Car {Id = 2, Make = "Volkswagen", Model = "Polo", Year = 2000 },
        new Car {Id = 3, Make = "Nissan", Model = "Micra", Year = 2005 },
        new Car {Id = 4, Make = "Bugatti", Model = "Veyron", Year = 2010 },
        new Car {Id = 5, Make = "Ferrari", Model = "Spider 458", Year = 2011 },
        new Car {Id = 6, Make = "Aston Martin", Model = "Vanquish", Year = 2010 },
        new Car {Id = 7, Make = "Ford", Model = "Focus", Year = 2001 },
        new Car {Id = 8, Make = "Fiat", Model = "Punto", Year = 2013 },
        new Car {Id = 9, Make = "Peugeot", Model = "206", Year = 2006 },
        new Car {Id = 10, Make = "Opel", Model = "Ascona", Year = 2002 },
        new Car {Id = 11, Make = "Volkswagen", Model = "Golf", Year = 1997 }
    };


    foreach (var car in cars)
    {
        car.SearchQuery = car.Make + " " + car.Model;
        _documentSession.Store(car);
    }

    _documentSession.SaveChanges();
}

问题是当 make 包含两个词时:“Aston Martin”等。有人可以填写我所缺少的内容吗?

4

1 回答 1

1

我相信这是因为你的Lucene。仅使用 FieldIndexing.Analyzed 就假定您要使用 StandardAnalyzer 进行标记。因此,当您传入多个单词时,它会找到与这些单词中的任何一个匹配的任何内容。

由于您希望所有单词都匹配,因此您可以执行以下操作

var search = string.Join(" ", query
      .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
      .Select(x => string.Format("{0}* ", x)));

query =
    _documentSession.Query<Car>("CarIndex")
            .Statistics(out stats)
            .Search(x => x.SearchQuery, search, 10, SearchOptions.And, EscapeQueryOptions.AllowPostfixWildcard)

或者你可以改变你的分析仪

http://ravendb.net/docs/client-api/querying/static-indexes/configuring-index-options

于 2013-06-06T18:53:00.167 回答