How to do a multi query search in Lucene 7.4.0? - java

I have two queries, one of them is boosted, and I want to combine them into one new query. I understand that in older version of Lucene you can do it with BooleanQuery using add. But in the version I'm using, that method seems to no longer exists. So how do I do it now?

To add queries into a 'BooleanQuery', now I have to use the 'Builder'. So if I want to add queries, the code line should be something like new BooleanQuery().Builder().add(query1).add(query2).build()
Yes, I should've read the migration guide first.
Also if I want to combine a boosted query and a normal one, I could just concate the strings.

Related

Groupping results based on fields - Lucene

I'm using Lucene 4.10.4. I want to take "n results" from 20 different fields in an efficient way without searching 20 times. If I search using boolean query, we might get all the results in single search. I want to group results based on fields, is there any grouping concept?
Yes there is:
http://lucene.apache.org/core/4_10_4/grouping/org/apache/lucene/search/grouping/package-summary.html
But in newer versions it only works on DocValues, so you would have to add the field again as a DocValue to be able to group over it. (But maybe in 4.10 it still works with the FieldCache, but I'm not familiar with that)
You can use GroupingSearch or maybe BlockGroupingCollector to have multiple elements per group and specify how results are ordered within a group.
You have to include the lucene-grouping dependency to use it.

Is there a way to use redquerybuilder with Lucene?

Is there any interface to Lucene from redquery builder (https://github.com/salk31/RedQueryBuilder)? I have a Lucene indexed system and would like to support complex Lucene queries using its AND and NOT operators.
Alternatively, are there other visual query builders out there for Lucene that support "advanced search" functionality that include arbitrary numbers of AND/OR clauses?
As far as I know there isn't one that exists already.
I can think of two options:
* Use a SQL parser and serialiser to transform SQL into Lucence syntax (or query objects on the server)
* Use the 0.8.0 custom serialiser in RQB to produce Lucence query string. NB This is very experimental feature.
I should declare that I started the RQB project and that I love Lucence. Maybe worth raising a ticket for a feature? It could be a good way to test the serialisation if it produces Lucence queries rather than SQL?
NB If you want to store/re-load queries RQB is only designed to use SQL. It was chosen as a standard language to load/save even if never gets executed.

Insert Query Builder for java

I have a use case where in I need to read rows from a file, transform them using an engine and then write the output to a database (that can be configured).
While I could write a query builder of my own, I was interested in knowing if there's already an available solution (library).
I searched online and could find jOOQ library but it looks like it is type-safe and has a code-gen tool so is probably suited for static database schema's. In the use case that I have db's can be configured dynamically and the meta-data is programatically read and made available for write-purposes (so a list of tables would be made available, user can select the columns to write and the insert script for these column needs to be dynamically created).
Is there any library that could help me with the use case?
If I understand correctly you need to query the database structure, display the result to via a GUI and have the user map data from a file to that structure?
Assuming this is the case, you're not looking for a 'library', you're looking for an ETL tool.
Alternatively, if you're set on writing something yourself, the (very) basic way to do this is:
the structure of a database using Connection.getMetaData(). The exact usage can vary between drivers so you'll need to create an abstraction layer that meets your needs - I'd assume you're just interested in the table structure here.
the format of the file needs to be mapped to a similar structure to the tables.
provide a GUI that allows the user to connect elements from the file to columns in the table including any type mapping that is needed.
create a parametrized insert statement based on file element to column mapping - this is just a simple bit of string concatenation.
loop throw the rows in the file performing a batch insert for each.
My advice, get an ETL tool, this sounds like a simple problem, but it's full of idiosyncrasies - getting even an 80% solution will be tough and time consuming.
jOOQ (the library you referenced in your question) can be used without code generation as indicated in the jOOQ manual:
http://www.jooq.org/doc/latest/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
When searching through the user group, you'll find other users leveraging jOOQ in the way you intend
The setps you need to do is:
read the rows
build each row into an object
transform the above object to target object
insert the target object into the db
Among the above 4 steps, the only thing you need to do is step 3.
And for the above purpose, you can use Transmorph, EZMorph, Commons-BeanUtils, Dozer, etc.

Performance of search in java list vs on database records using hibernate

Now I have a situation where I need to make some comparisons and result filtration that is not very simple to do, what I want is something like Lucenes search but only I will develop it, it is not my decision though I would have gone with Lucene.
What I will do is:
Find the element according to full word match of a certain field, if not then check if it starts with it the check if it just contains.
Every field has its weight according to matching case(full->begins->contains) and its priority to me.
After one has matched I will also check the weight of the other fields as well to make a final total row weight.
Then I will return an Map of both rows and their weights.
Now I realized that this is not easy done by hibernate's HQL meaning I would have to run multiple queries to do this.
So my question is should I do it in java meaning should I retrieve all records and do my calculations to find my target, or should I do it in hibernate by executing multiple queries? which is better according to performance and speed ?
Unfortunately, I think the right answer is "it depends": how many words, what data structure, whether the data fits in memory, how often you have to do the search, etc.
I am inclined to think that a database is a better solution, even if Hibernate is not part of it. You might need to learn how to write better SQL. Perhaps the dynamic SQL that Hibernate generates for you isn't sufficient. Proper JOINs and indexing might make this perform nicely.
There might be a third way to consider: Lucene and indexing. I'd need to know more about your problem to decide.

How to build a Datasource with several queries in the JRXML?

I need to know how to generate reports dynamically in jasper reports? In my case table has id,name,design as fields. I need to manipulate five different queries in one jrxml file.
First one will select the entire table.
Second one will select id,name alone.
Third one name alone.
Here i succeed with selecting the entire table but am getting confused on how to to run the rest.
You have 3 choices:
1) You can combine all 5 queries into a single query. This can be difficult (sometimes impossible) to do, but should be tried first. Generally speaking the data in your report is related in some way to the other data your report. It would seem pointless to have completely unrelated data thrown together in rows in a report. So you should be able to do this.
This is your best option. You should be able to make those 5 queries into one single query. It will make your life easier, maintenance easer, and not to mention the initial design easier. You will be able to do everything in a single jrxml. You can do grouping, and formatting, and sorting, etc all in the jrxml. So look closely into this. If you are having trouble, create a new question on this site, give the queries, and the table descriptions, and I bet some SQL experts here will give you hand.
2) You can forgo placing the query in the JRXML altogether, and instead pass in a datasource through java that contains all the data. Check out Using a POJO as an IReport Datasouce for an example of how you could do this.
This is not that terrible of an option if you cannot do option 1 above. It is a decent compromise, but you are still running several queries to build your datasource, and that will have a negative effect of the time it takes to actually run the report.
3) You can use sub-reports, so that each report has one query, that you can use the result from the previous query to run the next. This would end up giving you a tree hierarchy of reports, and in your case would mean creating 5 reports.
I do not think this is a good idea in your case, but just wanted to make sure your are aware it is an option. It is going to be overly complex to setup, and difficult to maintain.
In addition to the three options presented by jschoen, there is a fourth option:
4) Use subdatasets to run multiple queries in one report. You can see the answer to Multiple queries in a single jasper document to see how to do this.

Categories

Resources