Inheritance: Lucene.Net.Analysis.PerFieldAnalyzerWrapper
Example #1
0
        // Lucene Query creation logic

        static Query ConstructQuery(Dictionary <string, HashSet <string> > clauses)
        {
            BooleanQuery query = new BooleanQuery();

            using var analyzer = new PackageAnalyzer();

            foreach (var clause in clauses)
            {
                switch (clause.Key.ToLowerInvariant())
                {
                case "id":
                    IdClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "packageid":
                    PackageIdClause(query, analyzer, clause.Value);
                    break;

                case "version":
                    VersionClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "title":
                    TitleClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "description":
                    DescriptionClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "tag":
                case "tags":
                    TagClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "author":
                case "authors":
                    AuthorClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "summary":
                    SummaryClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                case "owner":
                case "owners":
                    OwnerClause(query, analyzer, clause.Value, Occur.MUST);
                    break;

                default:
                    AnyClause(query, analyzer, clause.Value);
                    break;
                }
            }

            return(query);
        }
Example #2
0
 private static void AddToIndex(Directory directory, IEnumerable <IPackageSearchMetadata> entries)
 {
     using var packageAnalyzer = new PackageAnalyzer();
     using var writer          = new IndexWriter(directory, packageAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);
     foreach (var document in entries.Select(CreateDocument))
     {
         writer.AddDocument(document);
     }
     writer.Commit();
 }
        public void AddsCorrectFieldAnalyzers(string field, string text, TokenAttributes[] expected)
        {
            // arrange
            var analyzer = new PackageAnalyzer();

            // act
            var tokenStream = analyzer.TokenStream(field, new StringReader(text));
            var actual = tokenStream.Tokenize().ToArray();

            // assert
            Assert.Equal(expected, actual);
        }
        // Lucene Query creation logic

        private static Query ConstructQuery(Dictionary<QueryField, HashSet<string>> clauses, OwnersResult owners)
        {
            Analyzer analyzer = new PackageAnalyzer();

            List<Filter> filters = new List<Filter>();

            BooleanQuery booleanQuery = new BooleanQuery();
            foreach (var clause in clauses)
            {
                switch (clause.Key)
                {
                case QueryField.Id:
                    IdClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.PackageId:
                    PackageIdClause(booleanQuery, analyzer, clause.Value);
                    break;
                case QueryField.Version:
                    VersionClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Title:
                    TitleClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Description:
                    DescriptionClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Tag:
                    TagClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Author:
                    AuthorClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Summary:
                    SummaryClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;
                case QueryField.Owner:
                    if (owners != null)
                    {
                        filters.AddRange(OwnerFilters(owners, clause.Value));
                    }
                    break;
                default:
                    AnyClause(booleanQuery, analyzer, clause.Value);

                    if (owners != null)
                    {
                        var ownerFilters = OwnerFilters(owners, clause.Value).ToList();
                        if (ownerFilters.Any())
                        {
                            booleanQuery.Add(ConstructFilteredQuery(new MatchAllDocsQuery(), ownerFilters), Occur.SHOULD);
                        }
                    }

                    break;
                }
            }

            // Determine if we have added any clauses - if not, match all docs
            Query query = booleanQuery;
            if (!booleanQuery.Clauses.Any())
            {
                query = new MatchAllDocsQuery();
            }

            // Any filters to add?
            query = ConstructFilteredQuery(query, filters);

            return query;
        }
Example #5
0
        // Lucene Query creation logic

        private static Query ConstructQuery(Dictionary <QueryField, HashSet <string> > clauses, OwnersResult owners)
        {
            Analyzer analyzer = new PackageAnalyzer();

            List <Filter> filters = new List <Filter>();

            BooleanQuery booleanQuery = new BooleanQuery();

            foreach (var clause in clauses)
            {
                switch (clause.Key)
                {
                case QueryField.Id:
                    IdClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.PackageId:
                    PackageIdClause(booleanQuery, analyzer, clause.Value);
                    break;

                case QueryField.Version:
                    VersionClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Title:
                    TitleClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Description:
                    DescriptionClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Tag:
                    TagClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Author:
                    AuthorClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Summary:
                    SummaryClause(booleanQuery, analyzer, clause.Value, Occur.MUST);
                    break;

                case QueryField.Owner:
                    if (owners != null)
                    {
                        filters.AddRange(OwnerFilters(owners, clause.Value));
                    }
                    break;

                default:
                    AnyClause(booleanQuery, analyzer, clause.Value);

                    if (owners != null)
                    {
                        var ownerFilters = OwnerFilters(owners, clause.Value).ToList();
                        if (ownerFilters.Any())
                        {
                            booleanQuery.Add(ConstructFilteredQuery(new MatchAllDocsQuery(), ownerFilters), Occur.SHOULD);
                        }
                    }

                    break;
                }
            }

            // Determine if we have added any clauses - if not, match all docs
            Query query = booleanQuery;

            if (!booleanQuery.Clauses.Any())
            {
                query = new MatchAllDocsQuery();
            }

            // Any filters to add?
            query = ConstructFilteredQuery(query, filters);

            return(query);
        }