最佳实践建立一个搜索应用程序? [英] Best Practices for Building a Search App?

查看:96
本文介绍了最佳实践建立一个搜索应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将很快开始一个简单的数据存储和搜索项目。基本上,那些把我的巨大的Excel US preadsheet到数据库,构建一个Web GUI它,并使其可搜索类型的东西。

I will be starting a simple datastore-and-search project soon. Basically, one of those "put my huge Excel spreadsheet into a database, build a web GUI for it, and make it searchable" type-things.

有一件事情一直缠着我是当用户输入的一些标准将被用于实际的搜索逻辑。我想象与文本字段的搜索界面,和一些其他的过滤工具 - 下拉组合框和复选框和这样

One thing that's been bugging me is the actual search logic that will be used when the user enters some criteria. I am imagining a search interface with a text field, and a few other filtering tools - drop down combo boxes and check boxes and such.

虽然这让我过,我可以执行过滤非常强,精细的控制,我想知道什么SO的心思都放在实际执行搜索。我将使用ASP.NET,MS SQL Server和LINQ到SQL这里,所以让那些技术记在心里。

While that gives me very strong, granular control over the filtering I can perform, I am wondering what SO's thoughts are on actually performing the search. I'll be using ASP.NET, MS SQL Server, and Linq-To-SQL here, so keep those technologies in mind.

关闭我的头顶,我想我会做这样的事情:

Off the top of my head, I think I'd do something like:

var results = from s in db.Stuff
              where (s.Prop1.Contains(textFilter) ||
                     s.Prop2.Contains(textFilter) ||
                     s.Prop3.Contains(textFilter)) &&
                     checkbox1.IsChecked ?
                          s.Prop4.ToLower().Equals(combobox1.Text) : true
              select s;

这是我知道的:


  • 怎么办分组和联接如果有必要

  • 我可以使用的包含()的个人特性的方法生成SQL的 LIKE 的查询

  • 我可以过滤的东西财产由属性,构建如上面我的搜索逻辑。

  • How to do grouping and joins if necessary
  • I can use the Contains() method on individual properties to generate SQL LIKE queries
  • I can filter things property-by-property, building my search logic as above.

下面是我在问什么:


  • 有没有一种方法来搜索所有属性(不拉的所有对象到内存中 - 我假设意味着建立每个对象的与反思属性的列表,字符串化它们,然后检查不在)?如果不是,这似乎我不得不建立新的逻辑,为每一个新特性我想补充难以置信的繁琐。喜欢的 s.Contains(textFilter)东西的上面将是理想的。

  • 如何做一个SQL的 LIKE 的查询实际工作?那是什么我想做什么?

  • 是否有实施搜索规则,如用于全匹配和逻辑运算符,如引号的字符串的标准方法的的和的的?我会感到惊讶,如果每一个实现他们的应用程序定制解析逻辑这样做了。

  • 难道我叫错了树?我错过了什么?

  • Is there a way to search all properties (without pulling all objects into memory - which I assume means building a list of each object's properties with reflection, stringifying them, and then checking is out)? If not, this seems incredibly cumbersome as I'd have to build new logic for every new property I might add. Something like s.Contains(textFilter) in the above would be ideal.
  • How does a SQL LIKE query actually work? Is that something I want to do?
  • Is there a standard way of implementing search rules such as quoted strings for full-matching and logical operators such as AND and OR? I would be surprised if every application that implemented them did so with custom parsing logic.
  • Am I barking up the wrong tree? Did I miss something?

推荐答案

我最近创建一个评论系统类似的搜索。我所做的就是我创建了一些扩展方法关闭这让我在一个通用的过滤对象传递的评论。

I had to create a similar search for a comments system recently. What I did was I created some extension methods off of the comments which allowed me to pass in a generic filtering object.

下面是示例code,我用:

Here is the sample code that I used:

这仅仅是一个局部的方法,并且不会有回报,但它会给你我在做什么的图片:

This is just a partial method and does not have the return but it will give you a picture of what I am doing:

public List<oComment> GetComments(oCommentSearch filters)
{

    using (CommentDataContext db = CommentContextFactory.CreateContext())
    {
        var query = from comment in db.COMMENTs.FilterComments(filters)
                    select comment;
    }
}

正如你所看到关闭的评论我有FilterComments。这是一个扩展方法。这个方法看起来是这样的(这是整个类我有):

As you can see off the COMMENTs i have FilterComments. This is an extension method. This method looks like this (this is the entire class I have):

public static class CommentExtensions
    {
        public static IQueryable<COMMENT> FilterComments(this IQueryable<COMMENT> Comments, oCommentSearch Filters)
        {
            Filters = CheckFilter(Filters);

            IQueryable<COMMENT> tempResult = Comments;

            if(Filters.Classes.Count() > 0)
            {
                tempResult = from t in tempResult
                             where
                                 Filters.Classes.Contains(t.CLASS_ID)
                             select t;
            }

            if (Filters.Flags.Count() > 0)
            {
                tempResult = from t in tempResult
                             where
                                 Filters.Flags.Contains((int) t.FLAG_ID)
                             select t;
            }

            if (Filters.Types.Count() > 0)
            {
                tempResult = from t in tempResult
                             where
                                 Filters.Types.Contains(t.CommentTypeId)
                             select t;
            }
            return tempResult;
        }

        private static oCommentSearch CheckFilter(oCommentSearch Filters)
        {
            Filters.Classes  = CheckIntArray(Filters.Classes);
            Filters.Flags =  CheckIntArray(Filters.Flags) ;
            Filters.Types =  CheckIntArray(Filters.Types) ;
            return Filters;
        }

        private static int[] CheckIntArray(int[] ArrayToCheck)
        {
            return ArrayToCheck == null || ArrayToCheck.Count() == 0 ? new int[] {} : ArrayToCheck;
        }
    }

这应该让你在正确的方向开始为你正在尝试做的。

This should get you started in the right direction for what you are trying to do.

希望这有助于!

这篇关于最佳实践建立一个搜索应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆