Dim qp1 As New QueryParser("filename", New StandardAnalyzer())
Dim qp2 As New QueryParser("filetext", New StandardAnalyzer())
.
.
I am using the 'Lucene.Net' library and have the following question.
Instead of creating two separate QueryParser
objects and using them to obtain two Hits objects, is it possible perform a search on both fields using a single QueryParser
object, so that I have only one Hits object which gives me the overall score of each Document?
Just build a query string with each term:
It doesn't matter what you pass as the initial field in QueryParser's constructor. Just make sure you call .Parse() on your query string to get a Query object back to execute.
If you want to use an "and" search:
** you can also use
MultiFieldQueryParser
to search in all available fields.**E.g
here is complete an example.
hope that help
for each field create a query from the above queryparsers, then add the query to a booleanquery stating that it "must" occur.
Alternatively, check out the MultiFieldQueryParser, which is a simplified way of doing it.
There are 3 ways to do this.
The first way is to construct a query manually, this is what
QueryParser
is doing internally. This is the most powerful way to do it, and means that you don't have to parse the user input if you want to prevent access to some of the more exotic features ofQueryParser
:The second way is to use
MultiFieldQueryParser
, this behaves likeQueryParser
, allowing access to all the power that it has, except that it will search over multiple fields.The final way is to use the special syntax of
QueryParser
see here.Your other option is to create new field when you index your content called filenameandtext, into which you can place the contents of both filename and filetext, then you only have to search one field.