How to implement Full text search with Lucene tool for Derby Database - derby

Classpath for running the luceneSupport optional tool:-
Before I run the luceneSupport optional tool, set the classpath
contains the following jar files:
derby.jar
derbyoptionaltools.jar
core: The core Lucene machinery. For Lucene 4.5.0, this is
lucene-core-4.5.0.jar.
analyzers-common: The common Lucene analyzers. For Lucene 4.5.0, this is
lucene-analyzers-common-4.5.0.jar.
queryparser: The basic Lucene logic for query-parsing. For Lucene 4.5.0, this is
lucene-queryparser-4.5.0.jar.
First all I have created a database and inserted some values..
connect 'jdbc:derby:musetitles;create=true';
create schema MUSE;
set schema MUSE;
create table titles (ID int generated always as identity, ISBN varchar(16), PRINTISBN varchar(16), title varchar(1024), subtitle varchar(1024), author varchar(1024), series varchar(1024), publisher varchar(1024), collections varchar(128), collections2 varchar(128));
insert into titles (ISBN, PRINTISBN, TITLE, SUBTITLE, AUTHOR, SERIES, PUBLISHER, COLLECTIONS, COLLECTIONS2) values ('9780823254859','9780823254811','American Dictators','Reading Rousseau through Fanon','Jane Anna Gordon teaches Political Science and African-American Studies at the University of Connecticut at Storrs. ','Just Ideas','Fordham University Press','2014 Complete','2014 Philosophy and Religion');
insert into titles (ISBN, PRINTISBN, TITLE, SUBTITLE, AUTHOR, SERIES, PUBLISHER, COLLECTIONS, COLLECTIONS2) values ('9780823255313','9780823255283','Shaping the Future of African American Film','','Kelly A. Parker is Professor of Philosophy, Environmental Studies, & Liberal Studies at Grand Valley State University.','American Philosophy','Fordham University Press','2014 Complete','2014 Philosophy and Religion');
insert into titles (ISBN, PRINTISBN, TITLE, SUBTITLE, AUTHOR, SERIES, PUBLISHER, COLLECTIONS, COLLECTIONS2) values ('9780826353771','9780826353764','Buen Gusto and Classicism in the Visual Cultures of Latin America, 1780-1910','','Paul B. Niell','','University of New Mexico Press','2014 Complete','2014 History');
insert into titles (ISBN, PRINTISBN, TITLE, SUBTITLE, AUTHOR, SERIES, PUBLISHER, COLLECTIONS, COLLECTIONS2) values ('9780813563749','9780813563732','Treating AIDS','Politics of Difference, Paradox of Prevention','Thurka Sangaramoorthy','','Rutgers University Press','2014 Complete','2014 Political Science and Policy Studies');
insert into titles (ISBN, PRINTISBN, TITLE, SUBTITLE, AUTHOR, SERIES, PUBLISHER, COLLECTIONS, COLLECTIONS2) values ('9780870717215','9780870717208','Accomplishing NAGPRA','Perspectives on the Intent, Impact, and Future of the Native American Graves Protection and Repatriation Act','Edited by Sangita Chari and Jaime M. N. Lavallee','First Peoples: New Directions in Indigenous Studies','Oregon State University Press','2014 Complete','2014 Political Science and Policy Studies');
ALTER TABLE "MUSE"."TITLES" ALTER COLUMN "ID" NOT NULL;
ALTER TABLE "MUSE"."TITLES" ADD CONSTRAINT "PK_TITLES" PRIMARY KEY ("ID");
call syscs_util.syscs_register_tool('luceneSupport',true);
create procedure LuceneSupport.createIndex(schemaname varchar( 128 ), tablename varchar( 128 ),textcolumn varchar( 128 ))parameter style java modifies sql data language java external name 'org.apache.derby.impl.optional.lucene.LuceneSupport.createIndex';
CALL LUCENESUPPORT.CREATEINDEX( 'MUSE', 'TITLES', 'TITLE' ,null);
SELECT * FROM TABLE( LUCENESUPPORT.LISTINDEXES() ) T;
select * from table(MUSE.TITLES__TITLE('american -mexican',4, null)) t;
}
And I want to put full text search on multiple column in a same table while
I have tried many option but not success-ed. please help me out this problem.

Related

Oracle SQL Developer - How to add constraints to er diagram

I need to create an ER diagram using oracle SQL developer, I have created it, but I am struggling to add the constraints, does anyone have any advice on how to do this?
I am developing a hospital data model. Thanks
So reconsider your entities Think of it in terms of What "physical objects do you have and what can those objects do"
Start off by identifying them all and then combining like ones.
For example staff, dr, GP and patient are all "People" and share like information just There are just different "Types" of people so combine them!
Each ward has staff on it so you have People, Ward, and WardStaff
Each person could have address information.
A Person can be admitted to a ward
A persons who has been admitted can be treated by multiple drs and have multiple ailments
Remember if you have to update information in more than one place to keep it accurate it needs to be consolidated.
Consider Cardinality between the entities: Does a ward HAVE to have nurses assigned? is a Nurce always on a ward? Can a ward have zero, one or many nurses?
Can a patient have zero one or many ailments?
Can a dr have zero one or many patients?
Are dr's limited to wards?
Do Dr's have specialties too? (can they have more than one?)
Here's the entities I see after that read:
Ward
WardType
WardStaff
Admittance
Person
PersonType
Address
PatentAdmittanceAilments
AilmentType
TreatmentType
And then here's how I see them relate. Re-read the 4 pages and see if this looks right. ask what's wrong and ask what's missing. and is there too much?
Wards
WardID (int) PK
Name (varchar(10))
WardTypeID (int)
WardStaff
WardID (int) PK
StaffID (Varchar(6)) PK (Unique Constraint) as a nurse can only work in 1 ward
LeadEffective Date Shows when the nurse became lead of ward
LeadNurseID (varchar(6)) FK to PersonID
Admittance
AdmittanceID (int) PK
PatientID (VARCHAR(6))
WardID (Int) FK to Ward
AdmittanceDate (date)
DischargeDate (date)
Person
PersonID (varchar(6)) PK
PersonTypeID (Integer)
Name (varchar(30))
DOB Date (Date)
GPID (6,0)
AddressID (int) FK to Address
Address
AddressID (Int) PK
Address# (varchar(10))
BuildingName (varchar(30))
Unit# (varchar(10))
City (Varchar(50))
Street (varchar(50))
State (varchar(02))
Country (varchar(10))
ZipCode (varchar(10))
PatientAdmittanceAilments
AdmittanceID (Int) PK_1of3
DrID (varchar(06)) FK to Person
AilmentID (int) FK to Ailment PK2of3 A trigger needs ot be created to
ensure a compliantID
AilmentAdditional(varchar(40)) doesn't overlap the DTS/DTE
TreatmentID (int) FK to Treatment
TreatmentAdditional(varchar(40))
DTS (Date) PK3of3
DTE (Date)
PersonType
PersonTypeID (Int) PK
Description (Varchar(30)) (Examples: Staff, Patient, Dr, GP)
WardType
WardTypeID (int) PK
Description (varchar(20)) (Examples: Orthopaedic, geriatric...)
AilmentType
AilmentTypeID (int) PK
Description (varchar(40))
Treatments
TreatmentTypeID (int) PK
Description (varchar(40))

How to query distinct column value when query all row data

I hava one MySQL Table
id name birthdate city
1 Owen 2011/01/01 USA
2 Mark 2012/05/01 UK
3 Marry 2011/01/01 JP
4 John 2011/01/01 JP
First,I uesd jqgrid to read all row data. But Now,I want to know when birthdate=2011/01/01,how many different city in the table.
Can don't used sql,only used jqgrid plugin?
You are looking for distinct function.
SELECT DISTINCT(city) FROM table WHERE birthday = "2011/01/01";

Oracle display value replacement of flattened, delimited foreign key values

I working on a data export for a painfully denormalized COTS product and am hung up over how to plug display values in my selection for columns that contain a delimited string of foreign keys.
Assume the following sets of data for example.
DEPARTMENTS table:
Key Value
---------------------------------
1 Finance
2 Human Resources
3 Public Affairs
4 Information Technology
PERSONNEL table:
PK FName LName Departments
-------------------------------------------------
111 Marty Graw 1|~*~|3|~*~|
222 Rick Shaw 2|~*~|4|~*~|
333 Jean Poole 4|~*~|2|~*~|3|~*~|1|~*~|
Desired output from select:
FName LName Departments
-----------------------------------------------------------------------------------
Marty Graw Finance, Public Affairs
Rick Shaw Human Resources, Information Technology
Jean Poole Information Technology, Human Resources, Public Affairs, Finance
I've found examples of how to deal with delimited strings but nothing that really seems to fit this particular scenario. Ideally I'd like to figure out how I could do it without having to create functions etc. as my permissions are pretty limited.
This will not preserve the original order of the IDs, but if that's not important then this will work:
select DISTINCT
p.fname
,p.name
,LISTAGG(d.value, ', ')
WITHIN GROUP (ORDER BY d.value)
OVER (PARTITION BY p.pk)
AS departments_list
from personnel p
left join departments d
on INSTR('|~*~|'||p.departments||'|~*~|'
,'|~*~|'||d.key||'|~*~|') > 0;
SQL Fiddle: http://sqlfiddle.com/#!4/d292e/3/0
EDIT
If you really need them listed in the same order as the IDs, you can use this variant:
select DISTINCT
p.fname
,p.lname
,LISTAGG(d.value, ', ')
WITHIN GROUP (
ORDER BY INSTR('|~*~|'||p.departments||'|~*~|'
,'|~*~|'||d.key||'|~*~|'))
OVER (PARTITION BY p.pk) AS departments_list
from personnel p
left join departments d
on INSTR('|~*~|'||p.departments||'|~*~|'
,'|~*~|'||d.key||'|~*~|') > 0;
http://sqlfiddle.com/#!4/d292e/4

Relational Database - Finding instructors who taught the most courses in 2009

We have the following schema:
instructor(ID, name, dept name, salary)
teaches(ID, course id, sec id, semester, year)
Find instructors who taught the most courses in 2009. Can someone please help me? I'm confused how to write this out in relational algebra.
This must be homework ;-) So I'll give you some hints...
Since I haven't done tuple relational calculus since college (http://en.wikipedia.org/wiki/Relational_algebra), here is an approximation in sql,
select instructor.ID, instructor.name, count(teaches.ID)
from instructor
join teaches on teaches.ID = instructor.ID
and count(teaches.ID) >= ...
group by ...
Leaving you to fill in the group by and >= values.
Think about how you calculate how many courses each teacher teaches,
select teaches.ID, count(*)
from teaches
group by teaches.ID
This might help: MySQL count maximum number of rows

Include variable as field name in linq statement

Hi I have a link statement against a database table I did not create... The data structure is
Tbl_BankHols
BHDate .... Datetime
E ......... Bit
S ......... Bit
W ......... Bit
I ......... Bit
Basically it has a list of dates and then a value of 0 or 1 in E, S, W, I which indicate if that date is a bank holiday in England, Scotland, Wales and/or Ireland.
If I want to find out if a date is a bank holiday in any of the countries my Linq statement is
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate _
Select d.BHDate
Where chkDate is the date I am checking. If a result is returned then the date is a bank holiday in one of the countries.
I now need to find out if chkDate is a bank holiday in a particular country how do I introduce that into the where statement?
I'm asking if this is possible before I think about changing the structure of the database. I was thinking of just having a single country field as a string which will contain values like E, EW, EWS, EWSI, I and other similar combinations and then I just use WHERE BCountry LIKE %X% (where X is the country I'm interested in). Or is there a better way?
Erm stop,
Your suggestion for extra denormalisation and using LIKE is a really "wrong" idea.
You need one table for countries, lets call it Country and another table for holidays, lets call it Holiday. The Country table should contain a row for each country in your system/model. The Holiday table should have two columns. One for the Date and a foriegn key to country, lets call it CountryId.
Then your linq could look something like,
db.Holiday.Any(Function(h) h.Country.Name =
"SomeCountry" AndAlso h.Date = someDate)
The reasons why you shoudn't use LIKE for this are manifold but two major objections are.
LIKE doesn't perform well, its hard for an index to support it, and,
Lets imagine a situation where you need to store holidays for these countries,
Ecuador
El Salvador
Estonia
Ethiopia
England
Now, you have already assigned the code "E" to England, what code will you give to the others? No problem you say, "EL", "ET" ... but, already your LIKE "%E" condition is broken.
Here are the scripts for the schema I would go with.
CREATE TABLE [Country](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
(
[Id] ASC
));
CREATE TABLE [Holiday](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NOT NULL,
[CountryId] [int] NOT NULL FOREIGN KEY Country(Id),
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
(
[Id] ASC
));
Instead of changing the structure of your table the way you wrote, you could introduce a new Region (Code, Description) table and add a foreign key to your table pointing to the regions table. Your bank holidays table will then contain one record per (date/region) combination.
And your linq statement:
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate And d.Region = "England" _
Select d.BHDate
before I think about changing the structure of the database
You can do it by composing a query with OR predicates by using LINQKit.
Dim IQueryable<BankHoliday> BHQ = ... (your query)
Dim pred = Predicate.False(Of BankHolifday)
If countryString.Contains("E")
pred = pred.Or(Function(h) h.E)
EndIf
If countryString.Contains("S")
pred = pred.Or(Function(h) h.S)
EndIf
...
Return query.Where(pred.Expand())
(I'm not fluent in VB so there may be some error in there)
See this answer for a similar example.
While the DB structure is not optimal, if you are working with legacy code and there's no effort given for a full refactor, you're gonna have to make do (I feel for you).
The simplest option I think you have is to select not just the date, but also the attributes.
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate _
Select d
This code will give you d.S, d.E, d.W, and d.I, which you can use programmatically to determine if the holiday applies to whatever country you are currently working on. This means outside the query, you will have a separate if statement which would qualify if the holiday applies to the country you are processing.

Resources