如何计算来自数据库的搜索内容

Posted

技术标签:

【中文标题】如何计算来自数据库的搜索内容【英文标题】:How to count the Search content which coming from db 【发布时间】:2017-03-29 00:31:41 【问题描述】:

我找到了搜索词的描述,并使用 JQuery 自动完成功能得到了描述。现在我想显示每个描述的搜索词(单词)的计数。

<strike>
if ( !string.IsNullOrEmpty(searchTerm) )
        
            //searching description and name in TrainingTopic
            var modeltrainingtopic = db.TrainingTopicRepository.Get( ).Where(x => x.Description.ToLower( ).Contains(searchTerm.ToLower( )) || x.Name.ToLower( ).Contains(searchTerm.ToLower( ))).Select(x => new SearchViewModel  Id = x.Id, IsTrainingTopic=true, Description = x.Description, Name = x.Name, RedirectionLink = "" ).Take(10).ToList( );

            //searching description, content and name in SubTopic
            var modelsubtopic = db.SubTopicRepository.Get( ).Where(x => x.Description.ToLower( ).Contains(searchTerm.ToLower( )) || x.Name.ToLower( ).Contains(searchTerm.ToLower( )) || x.SubTopicContent.ToLower( ).Contains(searchTerm.ToLower( ))).Select(x => new SearchViewModel  Id = x.Id,IsSubTopic=true, Description = x.Description, Content = x.SubTopicContent, RedirectionLink = "" ).Take(10).ToList( );

            //searching description, content and name in SubSubTopic
            var modelsubsubtopic = db.SubSubTopicRepository.Get( ).Where(x => x.Description.ToLower( ).Contains(searchTerm.ToLower( )) || x.Name.ToLower( ).Contains(searchTerm.ToLower( )) || x.Content.ToLower( ).Contains(searchTerm.ToLower( ))).Select(x => new SearchViewModel  Id = x.Id,IsSubSubTopic=true, Name = x.Name, Description = x.Description, Content = x.Content, RedirectionLink = "" ).Take(10).ToList( );
            return modeltrainingtopic.Concat(modelsubtopic).Concat(modelsubsubtopic);
        
</strike>

在我的视图模型中,我添加了 StringCount。现在我想在每个对象(modeltrainingtopic、modelsubtopic、modelsubsubtopic)中添加搜索词的计数。下面我如何添加 jquery

$(function () 

    var loc = window.location.pathname.split('/')[1];
    $("#srch-term").autocomplete(
        source: function (request, response) 

            $.ajax(
                url: "/" + loc + "/api/ResourceLanding/SearchString?searchTerm=" + request.term,
                type: "Get",
                success: function (data) 
                    if (!data.length) 
                        var result = [
                         
                             id: 0,
                             label: 'No matches found'
                         
                        ];
                        response(result);
                    
                    else 
                        response($.map(data, function (item) 
                            return  label: item.Description, value: item.Description, Id:item.Id, IsTrainingTopic: item.IsTrainingTopic, IsSubTopic: item.IsSubTopic, IsSubSubTopic: item.IsSubSubTopic, Name: item.Name, Content: item.Content ;
                        ))
                    
                
            )
        
    ).autocomplete("instance")._renderItem = function (ul, item) 
        var icon;
        if (item.IsTrainingTopic) 
            icon = '<i class="fa fa-globe" aria-hidden="true"></i>'
        
        else if (item.IsSubTopic) 
            icon = '<i class="fa fa-cog" aria-hidden="true"></i>'
        
        else if (item.IsSubTopic) 
            icon = '<i class="fa fa-cogs" aria-hidden="true"></i>'
        
        if (icon !== undefined) 
            return $("<li>")
              .append("<div style='width:100%' data-id=" + item.value + ">" + icon + "  " + item.label + "</div>")
              .appendTo(ul);
        
        else 
            return $("<li>")
            .append("<div style='width:100%' data-id=" + item.value + ">" + item.label + "</div>")
            .appendTo(ul);
        
    ;
);

请帮帮我

【问题讨论】:

【参考方案1】:

冗长但也是另一种方式:

1.)

只需创建一个具有属性的类,您将从表中获得主列表“modeltrainingtopic”,如下所示:

    public class YourListItems
    
        public int Id  get; set; 
        public bool IsTrainingTopic  get; set; 
        public string Description  get; set; 
        public string Content  get; set; 
        public string Name  get; set; 
        public string RedirectionLink  get; set; 
        public int SearchCount  get; set; 

    

2.) 创建将返回“SearchTerm”字数的方法,例如:

 static int CountWords(string StringInWhichYouNeedToSearch,string SearchTerm)
 
       return Regex.Matches(StringInWhichYouNeedToSearch, SearchTerm).Count;
 

3.)

现在创建类“YourListItems”类型的列表对象。

        List<YourListItems> myFinalList = new List<YourListItems>();

4.)

现在为您从表“modeltrainingtopic”中获得的每一行列表迭代循环

        foreach (var SingleRow in modeltrainingtopic)
        

//It will count search term in your description , name and content

 int SearchCount = CountWords(SingleRow.Description, searchTerm) + CountWords(SingleRow.Name, searchTerm) + CountWords(SingleRow.Content, searchTerm);



//Will add row to new list object named myFinalList 
            myFinalList.Add(

              new YourListItems
              
                  Id = SingleRow.Id,
                  IsTrainingTopic = SingleRow.IsTrainingTopic,
                  Description = SingleRow.Description,
                  Content = SingleRow.Content,
                  Name = SingleRow.Name,
                  RedirectionLink = SingleRow.RedirectionLink,
                  SearchCount = SearchCount,

              

            );
        

【讨论】:

感谢您的回复。但我必须在创建的每个对象中存储搜索字数。我想知道如何编写代码。如果您有任何想法,请帮助我 我已经编辑了我的答案。请检查。希望它会帮助你。 那些我们存储为列表的方法兄弟..在列表中我们如何直接在描述中找到搜索词? 意味着...兄弟,您需要从“modeltrainingtopic.Description”中计算您的“searchTerm”吗? 兄弟,看后端代码。我从 3 表中获取数据。因为我从描述、名称和内容中获取了搜索词数据。我单独附加数据行。所以我想要计数,一行中有多少个搜索词。例如,在方法模型训练主题列表中,我想要该方法中每行第一行的搜索词计数【参考方案2】:

这是我问题的答案。

var topicData= db.TrainingTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower())).Select(x=>x).Take(10).ToList();
var modeltrainingtopic = topicData.Select(x => new SearchViewModel  Id = x.Id,StringCount= topicData.Where(s =>s.Id==x.Id&& s.Description.ToLower().Contains(searchTerm.ToLower())).Count()+ topicData.Where(s => s.Id == x.Id && s.Name.ToLower().Contains(searchTerm.ToLower())).Count(), IsTrainingTopic = true, Description = x.Description, Name = x.Name, RedirectionLink = "" ).ToList();

在这里,我拿了旧列表并比较了我的 id,然后计算每个列表中的搜索词

【讨论】:

以上是关于如何计算来自数据库的搜索内容的主要内容,如果未能解决你的问题,请参考以下文章

如何使用来自搜索的值填充访问数据库表单

如何根据来自 JSON 数据源的搜索为文本框提供建议

如何搜索来自另一个页面模型的串联名称列表?

搜索输入 GET/POST 数据无法理解来自 MySQL DB 的 utf8_unicode_ci 泰语数据

网站SEO优化,爬虫如何抓取数据与搜索引擎的工作原理

计算机如何感知大数据——聚类算法