Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field having multiple distinct values

Tags:

Am building a "Book search" API using Lucene. I need to index Book Name,Author, and Book category fields in Lucene index.

A single book can fall under multiple distinct book categories...for example:

BookName1 --fiction,humour,philosophy. BookName1 --fiction,science. BookName1 --humour,business. BookName4-humour and so on.....

User should be able to search all the books under a particular category say "homour".

Given this situation, how do i index above fields and build the query in lucene?

like image 589
user40907 Avatar asked Dec 30 '08 22:12

user40907


People also ask

Can we use 2 distinct in SQL?

Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns.

Can we use distinct for more than one column?

The DISTINCT clause is used in the SELECT statement to remove duplicate rows from a result set. The DISTINCT clause keeps one row for each group of duplicates. The DISTINCT clause can be applied to one or more columns in the select list of the SELECT statement.

How can I get distinct values of multiple columns in SQL?

Select with distinct on all columns of the first query. Select with distinct on multiple columns and order by clause. Count() function and select with distinct on multiple columns.

Can you use distinct twice?

No you can't use that, it will throw an error, but there are other alternatives where you can get your desired results. Show activity on this post. the easiest way to find this it is to just run the query.


1 Answers

You can have a field for a Lucene document occur multiple times. Create the document, add the values for the the name and author, then do the same for each category

  • create new lucene document
  • add name field and value
  • add author field and value
  • for each category:
    • add category field and value
  • add document to index

When you search the index for a category, it will return all documents that have a category field with the value you're after. The category should be a 'Keyword' field.

I've written it in english because the specific code is slightly different per lucene version.

like image 167
Doug Avatar answered Jan 01 '23 21:01

Doug