热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

思考Sphinx并搜索多个模型-ThinkingSphinxandsearchingmultiplemodels

Imlookingforawaytoperformasearchagainstmultiplemodels(seethispost),andgotacouple

I'm looking for a way to perform a search against multiple models (see this post), and got a couple of answers saying that Thinking Sphinx would be a good match for this kind of thing.

我正在寻找一种方法来对多个模型进行搜索(参见这篇文章),并得到了几个答案,说思维狮身人面像将是一个很好的匹配这种事情。

Indeed, it looks sweet, and it seems the application-wide search capability (ThinkingSphinx.search) is close to what I want. But the docs state this will return various kinds of model objects, depending on where a match was found.

事实上,它看起来很甜美,似乎应用程序范围的搜索功能(ThinkingSphinx.search)接近我想要的。但文档声明这将返回各种模型对象,具体取决于找到匹配的位置。

I have a models somewhat like this:

我的模特有点像这样:

  • Employee
  • Company
  • Municipality
  • County

Where employees are linked to County only though Company, which in turn is linked to a Municipality, which in turn is linked to the actual County.

员工只通过公司与县相关联,公司又与市政府相关联,而市政府又与实际的县相关联。

Now as a result from my search, I really only want Employee objects. For example a search for the string "joe tulsa" should return all Employees where both words can be found somewhere in the named models. I'll get some false positives, but at least I should get every employee named "Joe" in Tulsa county.

现在作为搜索的结果,我真的只想要Employee对象。例如,搜索字符串“joe tulsa”应返回所有Employees,其中两个单词都可以在命名模型中的某处找到。我会得到一些误报,但至少我应该让塔尔萨县的每个员工都叫“乔”。

Is this something that can be achieved with built in functionality of Thinking Sphinx?

这是可以通过Thinking Sphinx的内置功能实现的吗?

1 个解决方案

#1


3  

I think what you should do in this case is define associations for your Employee model (which you probably have already), e.g.:

我认为在这种情况下你应该做的是定义你的Employee模型的关联(你可能已经有),例如:

class Employee  :company
  has_one :county, :through => :company
  ...
end

class Company  :municipality
  ...
end

class Municipality 

Edit: I tested the multi-level has_one relationship, and it doesn't work like that. Seems to be fairly complex to model these 4 layers without denormalizing. I'll update if I come up with something. In any case, if you denormalize (i.e. add redundant foreign IDs to all models to your employees table), the associations are straightforward and you massively increase your index generation time. At the same time, it may involve more work to insure consistency.

编辑:我测试了多级has_one关系,它不能像那样工作。对这4层进行建模而不进行非规范化似乎相当复杂。如果我拿出一些东西,我会更新。在任何情况下,如果您进行非规范化(即向所有模型添加冗余外部ID到您的employees表),则关联很简单,并且您会大量增加索引生成时间。同时,它可能涉及更多工作以确保一致性。

Once the associations are set up, you can define the Sphinx index in your Employee model, like this:

设置关联后,您可以在Employee模型中定义Sphinx索引,如下所示:

define_index do
  indexes :name, :sortable => :true
  indexes company(:name),      :as => :company
  indexes municipality(:name), :as => :municipality
  indexes county(:name),       :as => :county
  ...
end

That way the columns in your associations are indexed as well, and Sphinx will automatically join all those tables together when building the index.

这样,关联中的列也会被编入索引,并且Sphinx会在构建索引时自动将所有这些表连接在一起。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • 巧用arguments在Javascript的函数中有个名为arguments的类数组对象。它看起来是那么的诡异而且名不经传,但众多的Javascript库都使用着它强大的功能。所 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 点击上方“新机器视觉”,选择加”星标”或“置顶”重磅干货,第一时间送达很早就想总结一下前段时间学习HALCON的心得,但由于其他的事情总是抽不出时间。去年有过一段时间的集中学习,做 ... [详细]
author-avatar
落地有声800_491_431
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有