简体   繁体   中英

Database Definition for Sphinx Search

Background

I am creating a MySQL database to store items such as courses where there may be many attributes to a single course. For example:

A single course may have any or all of the following attributes :

  • Title (varchar)
  • Secondary Title (varchar)
  • Description (text)
  • Date
  • Time
  • Specific Location (varchar; eg. White Hall Room 7)
  • General Location (varchar; eg. Las Vegas, NV)
  • Location Coords (floats; eg. lat, long)
  • etc.

The database is set up as follows:

A table storing specific course info:

courses table:

  • Course_ID (a Primary Key unique ID for each course)
  • Creator_ID (a unique ID for the creator)
  • Creation_Date (datetime of course creation)
  • Modified_Date (where this is the most recent timestamp the course was modified)

The table storing each courses multiple attributes is set up as follows:

course_attributes table:

  • Attribute_ID (a unique ID for each attribute)
  • Course_ID (reference to the specific course attribute is for)
  • Attribute (varchar definining the attribute; eg. 'title')
  • Value (text containing value of specified attribute; eg. 'Title Of My Course')

Desire

I would like to search this database using sphinx search. With this search, I have different fields weighing different amounts, for example: 'title' would be more important than 'description'.

Specific search fields that I wish to have are:

  • Title
  • Date
  • Location (string)
  • Location (geo - lat/long)

The Question

Should I define a View in Mysql to organize the attributes according to 'title', 'description', etc., or is there a way to define my sphinx.conf file to understand specific attributes?

I am open to all suggestions to solving this problem, whether it be rearrangement of the database/tables or the way in which I search.

Let me know if you need any additional details to help me find a solution.

Thanks in advance for the help

!--Update--!

OK, so after reading some of the answers, I feel that I should provide some additional information.

Latitude / Longitude

The latitude/longitude attributes are created by me internally after receiving the general location string. I can generate the values in any way I wish, meaning that I can store them together in a single lat/long attribute as 'float lat, float long' values or any other desired format. This is done only after they have been generated from the initial location string and verified. This is to guard against malformed data as @X-Zero and @Cody have suggested.

Keep in mind that the latitude and longitude was merely illustrating the need to have that field be searchable as opposed to anything more than that. It is simply another attribute; one of many.

Weighting Search Results

I know how to add weights to results in a Sphinx search query:

$cl->setFieldWeights( array('title'=>1000, 'description'=>500) );

This causes the title column to have a higher weight than the description column if the structure was as @X-Zero suggested. My question was more directed to how one would apply the above logic with the current table definition.

Database Structure, Views, and Efficiency

Using my introductory knowledge of Views , I was thinking that I could possibly create something that displays a row for each course where each attribute is its own column. I don't know how to accomplish this or if it's even possible.

I am not the most confident with database structures, but the reason I set my tables up as described was because there are many cases where not all of the fields will be completed for every course and I was attempting to be efficient [yes, it seems as though I've failed].

I was thinking that using my current structure, each attribute would contain a value and would therefore cause no wasted space in the table. Alternatively, if I had a table with tons of potential attributes, I would think there would be wasted space. If I am incorrect, I am happy to learn why my understanding is wrong.

Let me preface this by saying that I've never even heard of Sphinx, nor (obviously) used it. However, from a database perspective...

Doing multi-domain columns like this is a terrible ( I will hunt you down and kill you ) idea. For one thing, it's impossible to index or sort meaningfully, period. You also have to pray that you don't get a latitude attribute with textual data (and because this can only be reinforced programatically, I'm going to garuantee this will happen) - doing so will cause all distance based formulas to crash. And speaking of location, what happens if somebody stores a latitude without a longitude (note that this is possible regardless of whether you are storing a single GeoLocation attribute, or the pair)?

Your best bet is to do the following:

  1. Figure out which attributes will always be required. These belong in the course table (...mostly).
  2. For each related set of optional attributes, create a table. For example, location (although this should probably be required...), which would contain Latitude/Longitude, City, State, Address, Room, etc. Allow the columns to be null able (in sets - add constraints so users can't add just longitude and not latitude ).
  3. For every set of common queries add a view. Even (perhaps especially) if you persist in using your current design, use a view. This promotes seperation between the logical and physical implementations of the database. (This assumes searching by SQL) You will then be able to search by specifying view_column is null or view_column = input_parameter or whichever.
  4. For weighted searching (assuming dynamic weighting) your query will need to use left join s (inside the view as well - please document this), and use prepared-statement host-parameters (just save yourself the trouble of trying to escape things yourself). Check each set of parameters (both lat and long, for example), and assign the input weighting to a new column (per attribute), which can be summed up into a 'total' column (which must be over some threshold).

EDIT:

Using views:
For your structure, what you would normally do is left join to the attributes table multiple times (one for each attribute needed), keying off of the attribute (which should really be an int FK to a table; you don't want both 'title' and 'Title' in there) and joining on course_id - the value would be included as part of the select. Using this technique, it would be simple to then get the list of columns, which you can then apparently weight in Sphinx.
The problem with this is if you need to do any data conversion - you are betting that you'll be able to find all conversions if the type ever changes. When using strongly typed columns, this is between trivial (the likelyhood is that you end up with a uniquely named column) to unnecessary (views usually take their datatype definitions from the fields in the query); with your architecture, you'll likely end up looking through too many false positives.

Database efficiency:
You're right, unfilled columns are wasted space. Usually, when something is optional(ish), that means you may need an additional table. Which is Why I suggested splitting off location into it's own table: this prevents events which don't need a location (... what?) from 'wasting' the space, but then forces any event that defines a location to specify all required information. There's an additional benefit about splitting it off this way: if multiple events all use the same location (... not at the same time, we hope), a cross-reference table will save you a lot of space. Way more than your attributes table ever could (you're still having to store the complete location per event, after all). If you still have a lot of 'optional' attributes, I hear that NoSQL is made for these kinds of things (but I haven't really looked into it). However, other than that, the cost of an additional table is trivial ; the cost of the data inside may not be, but the space required is weighed against the perceived value of the data stored. Remember that disk space is relatively cheap - it's developer/maintainer time that is expensive.

Side note for addresses:
You are probably going to want to create an address table. This would be completely divorced from the event information, and would include (among other things) the precomputed latitude/longitude (in the recommended datatype - I don't know what it is, but it's for sure not a comma-separated string). You would then have an event_address table that would be the cross-reference between the events and where they take place - if there is additional information (such as room ), that should be kept in a location table that is referenced (instead of referencing address directly). Once a lat/long value is computed, you should never need to change it.

Thoughts on later updates for lat/long:
While specifying the lat/long values yourself is better, you're going to want to make them a required part of the address table (or part of/in addition to a purely lat/long only table). Frankly, multi-value columns (delimited lists) of any sort are just begging for trouble - you keep having to parse them every time you search on them (among other related issues). And the moment you make them separate rows, one of the pair will eventually get dropped - Murphy himself will personally intervene, if necessary. Additionally, updating them at different times from the addresses will result in an address having a lat/long pair that does not match; your best bet is to compute this at insertion time (there are a number of webservices to find this information for you).

Multi-domain tables:
With a multi-domain table, you're basically betting that the domain key ( attribute ) will never become out-of-sync with the value (err, value ). I don't care how good you are, somewhere, somehow, it's going to happen: at my company, we had one of these in our legacy application (it stored FK links and which files the FKs refer to, along with an attribute). At one point an application was installed in production which promptly began storing the correct file links, but the FK links to a different file , for a given class of attribute. Thankfully, there were audit records in another file which allowed this to be reversed (... as near as they were able tell).

In summary :
Revisit your required/optional data. Don't be afraid to create additional tables, each for a single entity, with every column for a single domain; you will also need relationship tables. You may also wish to place your audit data ( last_updated_time ) in a set of separate tables (single-domain tables will help immensely in this regard).

In the sphinx config you define your index and the SQL queries that populate it. You can define basic attributes, see Sphinx Attributes

Sphinx also supports geo searches on lat/long but they need to be expressed in radians, definitely not text columns like you have. I agree with X-Zero that storing lat/lng values are strings is a bad idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM