You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Jaakko Rajaniemi <ro...@kolumbus.fi> on 2010/12/13 15:31:27 UTC

Newbie: Indexing unrelated MySQL tables

Hello,

Alright, let's describe the situation. I have a website and the website has a database with at least three tables.

- "users" table  - (id, firstname, lastname)
- "artwork" table  - (id, user, name, description)
- "jobs" table  - (id, company, position, location, description)

I want to implement a multi-purpose search field. I want the search field to treat rows from each of these tables as independent results. For example if I type "Paris", an example result set might look like a list of links like this:

- Paris Hilton (from the "users" table)
- A day in Paris (from the "artwork" table)
- My Paris (from the "artwork" table)
- Art teacher (from the "jobs" table if the location is Paris)

I tried to search for solution for this on the Internet and found a somewhat similar thread floating on this mailing list, but I just couldn't understand it and the Solr documentation has its main focus on syntax, not implementation. I figured I would create three entities and relevant schema.xml entries in this way:

dataimport.xml:
<entity name="Users" query="select id,firstname,lastname from user"></entity>
<entity name="Artwork" query="select id,user,name,description from artwork"></entity>
<entity name="Jobs" query="select id,company,position,location,description from jobs"></entity>

schema.xml:
<field name="id" type="int" indexed="true" stored="true" required="true"/>
<field name="firstname" type="string" indexed="true" stored="true"/>
<field name="lastname" type="string" indexed="true" stored="true"/>
<field name="user" type="int" indexed="true" stored="true"/>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="description" type="text" indexed="true" stored="false"/>
<field name="company" type="string" indexed="true" stored="true"/>
<field name="position" type="string" indexed="true" stored="true"/>
<field name="location" type="string" indexed="true" stored="false"/>

This obviously does not work as I want. I only get results from the "users" table, and I cannot get results from neither "artwork" nor "jobs". I have found out that the possible solution is in putting <field> tags in the <entity> tag and somehow aliasing column names for Solr, but the logic behind this is completely alien to me and the blind tests I tried did not yield anything. My logic says that the "id" field is getting replaced by the "id" field of other entities and indexes are being overwritten. But if I aliased all "id" fields in all entities into something else, such as "user_id" and "job_id", I couldn't figure what to put in the <primaryKey> configuration in schema.xml because I have three different id fields from three different tables that are all primary keyed in the database!

Obviously I'm not quite on track so some help would be greatly appreciated. Thanks!
- Jaakko

Re: Newbie: Indexing unrelated MySQL tables

Posted by Erick Erickson <er...@gmail.com>.
Warning: I haven't tried this, but maybe it's relevant.
See: http://wiki.apache.org/solr/DataImportHandler
<http://wiki.apache.org/solr/DataImportHandler>particularly the "multiple
datasources" section. I'm thinking here that you have to
define a different data source for each separate table you want to extract.

Stefan's comments about using a transformer to make unique unique-ids per
table
seems spot on for that part of the problem....

Best
Erick

On Mon, Dec 13, 2010 at 9:31 AM, Jaakko Rajaniemi
<ro...@kolumbus.fi>wrote:

> Hello,
>
> Alright, let's describe the situation. I have a website and the website has
> a database with at least three tables.
>
> - "users" table  - (id, firstname, lastname)
> - "artwork" table  - (id, user, name, description)
> - "jobs" table  - (id, company, position, location, description)
>
> I want to implement a multi-purpose search field. I want the search field
> to treat rows from each of these tables as independent results. For example
> if I type "Paris", an example result set might look like a list of links
> like this:
>
> - Paris Hilton (from the "users" table)
> - A day in Paris (from the "artwork" table)
> - My Paris (from the "artwork" table)
> - Art teacher (from the "jobs" table if the location is Paris)
>
> I tried to search for solution for this on the Internet and found a
> somewhat similar thread floating on this mailing list, but I just couldn't
> understand it and the Solr documentation has its main focus on syntax, not
> implementation. I figured I would create three entities and relevant
> schema.xml entries in this way:
>
> dataimport.xml:
> <entity name="Users" query="select id,firstname,lastname from
> user"></entity>
> <entity name="Artwork" query="select id,user,name,description from
> artwork"></entity>
> <entity name="Jobs" query="select id,company,position,location,description
> from jobs"></entity>
>
> schema.xml:
> <field name="id" type="int" indexed="true" stored="true" required="true"/>
> <field name="firstname" type="string" indexed="true" stored="true"/>
> <field name="lastname" type="string" indexed="true" stored="true"/>
> <field name="user" type="int" indexed="true" stored="true"/>
> <field name="name" type="string" indexed="true" stored="true"/>
> <field name="description" type="text" indexed="true" stored="false"/>
> <field name="company" type="string" indexed="true" stored="true"/>
> <field name="position" type="string" indexed="true" stored="true"/>
> <field name="location" type="string" indexed="true" stored="false"/>
>
> This obviously does not work as I want. I only get results from the "users"
> table, and I cannot get results from neither "artwork" nor "jobs". I have
> found out that the possible solution is in putting <field> tags in the
> <entity> tag and somehow aliasing column names for Solr, but the logic
> behind this is completely alien to me and the blind tests I tried did not
> yield anything. My logic says that the "id" field is getting replaced by the
> "id" field of other entities and indexes are being overwritten. But if I
> aliased all "id" fields in all entities into something else, such as
> "user_id" and "job_id", I couldn't figure what to put in the <primaryKey>
> configuration in schema.xml because I have three different id fields from
> three different tables that are all primary keyed in the database!
>
> Obviously I'm not quite on track so some help would be greatly appreciated.
> Thanks!
> - Jaakko
>

Re: Newbie: Indexing unrelated MySQL tables

Posted by Alexey Serba <as...@gmail.com>.
> I figured I would create three entities and relevant
> schema.xml entries in this way:
>
> dataimport.xml:
> <entity name="Users" query="select id,firstname,lastname from
> user"></entity>
> <entity name="Artwork" query="select id,user,name,description from
> artwork"></entity>
> <entity name="Jobs" query="select id,company,position,location,description
> from jobs"></entity>
That's correct. You can list several entities under document element.
You can index them separately using entity parameter (i.e. add
entity=Users to you full import HTTP request). Do not forget to add
clean=false so you won't delete previously indexed documents. Or you
can index all entities in one request (by default).

> schema.xml:
> <field name="id" type="int" indexed="true" stored="true" required="true"/>
> <field name="firstname" type="string" indexed="true" stored="true"/>
> <field name="lastname" type="string" indexed="true" stored="true"/>
> <field name="user" type="int" indexed="true" stored="true"/>
> <field name="name" type="string" indexed="true" stored="true"/>
> <field name="description" type="text" indexed="true" stored="false"/>
> <field name="company" type="string" indexed="true" stored="true"/>
> <field name="position" type="string" indexed="true" stored="true"/>
> <field name="location" type="string" indexed="true" stored="false"/>
Why do you use string type for textual fields (description, company,
name, firstname, lastname, etc)? Is it intentional to use these fields
in filtering/faceting?

You can also add "default" searchable multivalued field (type=text)
and copy field instructions to copy all textual content into this
field ( http://wiki.apache.org/solr/SchemaXml#Copy_Fields ). Thus you
will be able to search in "default" field for terms from all fields
(firstname, lastname, name, description, company, position, location,
etc).

You would probably want to add field type=user/artwork/job. You will
be able to facet/filter on that fields and provide better user search
experience.

> This obviously does not work as I want. I only get results from the "users"
> table, and I cannot get results from neither "artwork" nor "jobs".
Are you sure that this is because the indexing isn't working? How do
you search for your data? What query parser (standard/dismax)/etc?

> I have
> found out that the possible solution is in putting <field> tags in the
> <entity> tag and somehow aliasing column names for Solr, but the logic
> behind this is completely alien to me and the blind tests I tried did not
> yield anything.
You don't need to list your fields explicitly in fields declaration.

BTW, what database do you use? Oracle has some issue with upper casing
column names that could be a problem.

> My logic says that the "id" field is getting replaced by the
> "id" field of other entities and indexes are being overwritten.
Are your ids unique across different objects? I.e. is there any job
with the same id as user? If so then you would probably want to prefix
your ids like:

<entity name="Users" query="select ('user_' || id) as
id,firstname,lastname from user"></entity>
<entity name="Artwork" query="select ('artwork_' || id) as
id,user,name,description from artwork"></entity>


> But if I
> aliased all "id" fields in all entities into something else, such as
> "user_id" and "job_id", I couldn't figure what to put in the <primaryKey>
> configuration in schema.xml because I have three different id fields from
> three different tables that are all primary keyed in the database!
You can still create separate id fields if you need to search for
different objects by id and don't mess with prefixed ids. But it's not
required.

HTH,
Alexey

Re: Newbie: Indexing unrelated MySQL tables

Posted by Stefan Matheis <ma...@googlemail.com>.
And yes, sorry for the short answer ..
http://wiki.apache.org/solr/DataImportHandler#TemplateTransformer would be
good for that :)

Re: Newbie: Indexing unrelated MySQL tables

Posted by Stefan Matheis <ma...@googlemail.com>.
To avoid overwrites in your case, use a combined id - f.e. $table_$id which
results in user_1, job_1 and so on ..