<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Examine;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Identity;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;

namespace Umbraco.Examine
{
    public class BackOfficeExamineSearcher : IBackOfficeExamineSearcher
    {
        private readonly IExamineManager _examineManager;
        private readonly ILocalizationService _languageService;
        private readonly ICurrentUserAccessor _currentUserAccessor;
        private readonly IEntityService _entityService;
        private readonly IUmbracoTreeSearcherFields _treeSearcherFields;

        public BackOfficeExamineSearcher(IExamineManager examineManager,
           ILocalizationService languageService,
           ICurrentUserAccessor currentUserAccessor,
           IEntityService entityService,
           IUmbracoTreeSearcherFields treeSearcherFields)
        {
            _examineManager = examineManager;
            _languageService = languageService;
            _currentUserAccessor = currentUserAccessor;
            _entityService = entityService;
            _treeSearcherFields = treeSearcherFields;
        }

        public IEnumerable<ISearchResult> Search(string query, UmbracoEntityTypes entityType, int pageSize, long pageIndex, out long totalFound, string searchFrom = null, bool ignoreUserStartNodes = false)
        {
            var sb = new StringBuilder();

            string type;
            var indexName = Constants.UmbracoIndexes.InternalIndexName;
            var fields = _treeSearcherFields.GetBackOfficeFields().ToList();

            // TODO: WE should try to allow passing in a lucene raw query, however we will still need to do some manual string
            // manipulation for things like start paths, member types, etc...
            //if (Examine.ExamineExtensions.TryParseLuceneQuery(query))
            //{

            //}

            //special GUID check since if a user searches on one specifically we need to escape it
            if (Guid.TryParse(query, out var g))
            {
                query = "\"" + g.ToString() + "\"";
            }

            var currentUser = _currentUserAccessor.TryGetCurrentUser();

            switch (entityType)
            {
                case UmbracoEntityTypes.Member:
                    indexName = Constants.UmbracoIndexes.MembersIndexName;
                    type = "member";
                    fields.AddRange(_treeSearcherFields.GetBackOfficeMembersFields());
                    if (searchFrom != null && searchFrom != Constants.Conventions.MemberTypes.AllMembersListId && searchFrom.Trim() != "-1")
                    {
                        sb.Append("+__NodeTypeAlias:");
                        sb.Append(searchFrom);
                        sb.Append(" ");<commit_msg>remove comments<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Examine;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Identity;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;

namespace Umbraco.Examine
{
    public class BackOfficeExamineSearcher : IBackOfficeExamineSearcher
    {
        private readonly IExamineManager _examineManager;
        private readonly ILocalizationService _languageService;
        private readonly ICurrentUserAccessor _currentUserAccessor;
        private readonly IEntityService _entityService;
        private readonly IUmbracoTreeSearcherFields _treeSearcherFields;

        public BackOfficeExamineSearcher(IExamineManager examineManager,
           ILocalizationService languageService,
           ICurrentUserAccessor currentUserAccessor,
           IEntityService entityService,
           IUmbracoTreeSearcherFields treeSearcherFields)
        {
            _examineManager = examineManager;
            _languageService = languageService;
            _currentUserAccessor = currentUserAccessor;
            _entityService = entityService;
            _treeSearcherFields = treeSearcherFields;
        }

        public IEnumerable<ISearchResult> Search(string query, UmbracoEntityTypes entityType, int pageSize, long pageIndex, out long totalFound, string searchFrom = null, bool ignoreUserStartNodes = false)
        {
            var sb = new StringBuilder();

            string type;
            var indexName = Constants.UmbracoIndexes.InternalIndexName;
            var fields = _treeSearcherFields.GetBackOfficeFields().ToList();

            //special GUID check since if a user searches on one specifically we need to escape it
            if (Guid.TryParse(query, out var g))
            {
                query = "\"" + g.ToString() + "\"";
            }

            var currentUser = _currentUserAccessor.TryGetCurrentUser();

            switch (entityType)
            {
                case UmbracoEntityTypes.Member:
                    indexName = Constants.UmbracoIndexes.MembersIndexName;
                    type = "member";
                    fields.AddRange(_treeSearcherFields.GetBackOfficeMembersFields());
                    if (searchFrom != null && searchFrom != Constants.Conventions.MemberTypes.AllMembersListId && searchFrom.Trim() != "-1")
                    {
                        sb.Append("+__NodeTypeAlias:");
                        sb.Append(searchFrom);
                        sb.Append(" ");
{'fraction_non_alphanumeric': 0.06915555555555555, 'fraction_numerical': 0.00035555555555555557, 'mean_word_length': 2.4430844553243576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7483814', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1366, 'n_words': 328}