Ejemplo n.º 1
0
        private static LuceneParams GetLuceneSearchParams(SearchArguments arguments)
        {
            var result = new LuceneParams();

            //Set Defaults
            if (arguments.Query == null)
            {
                arguments.Query = new MatchAllDocsQuery();
            }

            //Special Behavior for OData Queries since OData queries potentially specify the query/filter/skip/take all in one.
            var query = arguments.Query as ODataQuery;

            if (query != null)
            {
                var oDataQuery  = query;
                var parser      = new ODataQueryParser();
                var modelFilter = parser.ParseQuery(oDataQuery.DefaultField, oDataQuery.Query);

                result.Query = modelFilter.Query.IsNullOrWhiteSpace()
                           ? new Lucene.Net.Search.MatchAllDocsQuery()
                           : LuceneModelFilter.ParseQuery(oDataQuery.DefaultField, modelFilter.Query);

                result.Filter = modelFilter.Filter.IsNullOrWhiteSpace()
                            ? null
                            : LuceneModelFilter.ParseFilter(oDataQuery.DefaultField, modelFilter.Filter);

                result.Sort = modelFilter.Sort ?? new Lucene.Net.Search.Sort();

                if (modelFilter.Take > 0)
                {
                    result.MaxResults = modelFilter.Take;
                }

                if (modelFilter.Skip > 0)
                {
                    result.Skip = modelFilter.Skip;
                }
            }
            else
            {
                result.Query  = Barista.Search.Query.ConvertQueryToLuceneQuery(arguments.Query);
                result.Filter = null;
                result.Sort   = Barista.Search.Sort.ConvertSortToLuceneSort(arguments.Sort);

                if (arguments.Filter != null)
                {
                    result.Filter = Barista.Search.Filter.ConvertFilterToLuceneFilter(arguments.Filter);
                }

                if (arguments.Skip.HasValue)
                {
                    result.Skip = arguments.Skip.Value;
                }

                result.MaxResults = arguments.Take ?? 1000;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public int SearchResultCount(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
        {
            if (arguments == null)
            {
                arguments = new SearchArguments();
            }

            try
            {
                var index        = GetOrAddIndex(indexDefinition, true);
                var searchParams = GetLuceneSearchParams(arguments);

                IndexSearcher indexSearcher;
                using (index.GetSearcher(out indexSearcher))
                {
                    if (searchParams.Skip.HasValue == false)
                    {
                        var hits = indexSearcher.Search(searchParams.Query, searchParams.Filter, searchParams.MaxResults, searchParams.Sort);
                        return(hits.ScoreDocs.Count());
                    }
                    else
                    {
                        var hits = indexSearcher.Search(searchParams.Query, searchParams.Filter, searchParams.MaxResults + searchParams.Skip.Value, searchParams.Sort);
                        return(hits.ScoreDocs
                               .Skip(searchParams.Skip.Value)
                               .Take(searchParams.MaxResults)
                               .Count());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
        public int SearchResultCount(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
        {
            var response = ExecuteRestRequest <int>("SearchResultCount", Method.POST, request => request.AddBody(new
            {
                indexDefinition,
                arguments
            }));

            return(response.Data);
        }
Ejemplo n.º 4
0
        public IList <FacetedSearchResult> FacetedSearch(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
        {
            if (arguments == null)
            {
                arguments = new SearchArguments();
            }

            try
            {
                var index        = GetOrAddIndex(indexDefinition, true);
                var searchParams = GetLuceneSearchParams(arguments);

                IndexSearcher indexSearcher;
                using (index.GetSearcher(out indexSearcher))
                {
                    var reader = indexSearcher.IndexReader;

                    if (arguments.GroupByFields == null)
                    {
                        arguments.GroupByFields = new List <string>();
                    }

                    var facetedSearch = new SimpleFacetedSearch(reader, arguments.GroupByFields.ToArray());
                    var hits          = facetedSearch.Search(searchParams.Query, searchParams.MaxResults);
                    var result        = hits.HitsPerFacet
                                        .AsQueryable()
                                        .OrderByDescending(hit => hit.HitCount)
                                        .Select(facetHits => RetrieveFacetSearchResults(facetHits))
                                        .ToList();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
        private SearchArguments CoerceSearchArguments(object query, object maxResults, object groupByFields)
        {
            var args = new Barista.Search.SearchArguments();

            if (query == null || query == Null.Value || query == Undefined.Value)
            {
                args.Query = new MatchAllDocsQuery();
                if (maxResults != Undefined.Value && maxResults != Null.Value && maxResults != null)
                {
                    args.Take = JurassicHelper.GetTypedArgumentValue(Engine, maxResults, DefaultMaxResults);
                }
            }
            else if (TypeUtilities.IsString(query))
            {
                args.Query = new QueryParserQuery
                {
                    Query = TypeConverter.ToString(query)
                };

                if (maxResults != Undefined.Value && maxResults != Null.Value && maxResults != null)
                {
                    args.Take = JurassicHelper.GetTypedArgumentValue(Engine, maxResults, DefaultMaxResults);
                }

                if (groupByFields != null && groupByFields != Undefined.Value && groupByFields != Null.Value &&
                    groupByFields is ArrayInstance)
                {
                    args.GroupByFields = ((ArrayInstance)groupByFields)
                                         .ElementValues
                                         .Select(t => TypeConverter.ToString(t))
                                         .ToList();
                }
            }
            else
            {
                var instance = query as SearchArgumentsInstance;
                if (instance != null)
                {
                    var searchArgumentsInstance = instance;
                    args = searchArgumentsInstance.GetSearchArguments();
                }
                else if (query.GetType().IsAssignableFrom(typeof(IQuery <>)))
                {
                    args = new SearchArguments();

                    var pi = typeof(IQuery <>).GetProperty("Query");
                    args.Query = (Query)pi.GetValue(query, null);
                }
                else
                {
                    var obj = query as ObjectInstance;
                    if (obj != null)
                    {
                        var argumentsObj = obj;

                        args = new SearchArguments();

                        //Duck Type for the win
                        if (argumentsObj.HasProperty("query"))
                        {
                            var queryObj     = argumentsObj["query"];
                            var queryObjType = queryObj.GetType();

                            var queryProperty = queryObjType.GetProperty("Query", BindingFlags.Instance | BindingFlags.Public);
                            if (queryProperty != null && typeof(Query).IsAssignableFrom(queryProperty.PropertyType))
                            {
                                args.Query = queryProperty.GetValue(queryObj, null) as Query;
                            }
                        }
                        else
                        {
                            var queryObjType = obj.GetType();

                            var queryProperty = queryObjType.GetProperty("Query", BindingFlags.Instance | BindingFlags.Public);
                            if (queryProperty != null && typeof(Query).IsAssignableFrom(queryProperty.PropertyType))
                            {
                                args.Query = queryProperty.GetValue(obj, null) as Query;
                            }

                            if (maxResults != Undefined.Value && maxResults != Null.Value && maxResults != null)
                            {
                                args.Take = JurassicHelper.GetTypedArgumentValue(Engine, maxResults, DefaultMaxResults);
                            }
                        }

                        if (argumentsObj.HasProperty("filter"))
                        {
                            var filterObj     = argumentsObj["filter"];
                            var filterObjType = filterObj.GetType();

                            var filterProperty = filterObjType.GetProperty("Filter", BindingFlags.Instance | BindingFlags.Public);
                            if (filterProperty != null && typeof(Filter).IsAssignableFrom(filterProperty.PropertyType))
                            {
                                args.Filter = filterProperty.GetValue(filterObj, null) as Filter;
                            }
                        }

                        if (argumentsObj.HasProperty("groupByFields"))
                        {
                            var groupByFieldsValue = argumentsObj["groupByFields"] as ArrayInstance;
                            if (groupByFieldsValue != null)
                            {
                                args.GroupByFields = groupByFieldsValue
                                                     .ElementValues
                                                     .Select(t => TypeConverter.ToString(t))
                                                     .ToList();
                            }
                        }

                        if (argumentsObj.HasProperty("sort") && argumentsObj["sort"] is SortInstance)
                        {
                            var sortValue = (SortInstance)argumentsObj["sort"];
                            args.Sort = sortValue.Sort;
                        }

                        if (argumentsObj.HasProperty("skip"))
                        {
                            var skipObj = argumentsObj["skip"];
                            args.Skip = TypeConverter.ToInteger(skipObj);
                        }

                        if (argumentsObj.HasProperty("take"))
                        {
                            var takeObj = argumentsObj["take"];
                            args.Take = TypeConverter.ToInteger(takeObj);
                        }
                    }
                    else
                    {
                        throw new JavaScriptException(Engine, "Error", "Unable to determine the search arguments.");
                    }
                }
            }

            return(args);
        }
        public IList <FacetedSearchResult> FacetedSearch(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
        {
            var response = ExecuteRestRequest <List <FacetedSearchResult> >("FacetedSearch", Method.POST, request => request.AddBody(new
            {
                indexDefinition,
                arguments
            }));

            return(response.Data);
        }
 public IList <FacetedSearchResult> FacetedSearch(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
 {
     return(Channel.FacetedSearch(indexDefinition, arguments));
 }
 public int SearchResultCount(BaristaIndexDefinition indexDefinition, SearchArguments arguments)
 {
     return(Channel.SearchResultCount(indexDefinition, arguments));
 }