Ejemplo n.º 1
0
        public List <ResultData> Search(MultiFieldSearchViewModel mfsViewModel)
        {
            var         results            = new List <ResultData>();
            var         requiredHighlights = new List <string>();
            var         booleanQuery       = new BooleanQuery();
            IndexerType type = AnalyzerService.GetIndexerType(mfsViewModel.Language);

            InformationRetriever informationRetriever = new InformationRetriever(type, RAW_DIR_PATH, INDEX_DIR_PATH);

            QueryType     queryType     = (QueryType)Enum.Parse(typeof(QueryType), mfsViewModel.QueryType);
            QueryOperator queryOperator = (QueryOperator)Enum.Parse(typeof(QueryOperator), mfsViewModel.QueryOperator);
            Occur         occur         = queryOperator.Equals(QueryOperator.AND)
                ? Occur.MUST
                : Occur.SHOULD;

            try
            {
                if (!string.IsNullOrEmpty(mfsViewModel.Title))
                {
                    requiredHighlights.Add("Title");
                    booleanQuery.Add(QueryBuilder.BuildQuery(type, queryType, "Title", mfsViewModel.Title.Trim()), occur);
                }

                if (!string.IsNullOrEmpty(mfsViewModel.Author))
                {
                    requiredHighlights.Add("Author");
                    booleanQuery.Add(QueryBuilder.BuildQuery(type, queryType, "Author", mfsViewModel.Author.Trim()), occur);
                }

                if (!string.IsNullOrEmpty(mfsViewModel.Keywords))
                {
                    requiredHighlights.Add("Keyword");
                    List <Query> queries = BuildQueriesForKeywords(type, queryType, occur, mfsViewModel.Keywords);
                    queries.ForEach(x => booleanQuery.Add(x, occur));
                }

                if (!string.IsNullOrEmpty(mfsViewModel.Content))
                {
                    requiredHighlights.Add("Content");
                    booleanQuery.Add(QueryBuilder.BuildQuery(type, queryType, "Content", mfsViewModel.Content.Trim()), occur);
                }

                if (!string.IsNullOrEmpty(mfsViewModel.Language))
                {
                    requiredHighlights.Add("Language");
                    booleanQuery.Add(QueryBuilder.BuildQuery(type, queryType, "Language", mfsViewModel.Language.Trim()), occur);
                }

                results = informationRetriever.RetrieveEBooks(type, booleanQuery, requiredHighlights, Sort.INDEXORDER);
            }
            catch (Exception e)
            {
                results = null;
            }

            return(results);
        }