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 Subhash Bhushan <su...@stratalabs.in> on 2010/10/08 09:30:43 UTC
SOLRJ - Searching text in all fields of a Bean
Hi,
I have two fields in the bean class, id and title.
After adding the bean to SOLR, I want to search for, say "kitten", in all
defined fields in the bean, like this -- query.setQuery( "kitten"); --
But I get results only when I affix the bean field name before the search
text like this -- query.setQuery( "title:kitten"); --
Same case even when I use SolrInputDocument, and add these fields.
Can we search text in all fields of a bean, without having to specify a
field?
If we can, what am I missing in my code?
*Code:*
Bean:
-----------------------------------
public class SOLRTitle {
@Field
public String id = "";
@Field
public String title = "";
}
-----------------------------------
Indexing function:
-----------------------------------
private static void uploadData() {
try {
... // Get Titles
List<SOLRTitle> solrTitles = new
ArrayList<SOLRTitle>();
Iterator<Title> it = titles.iterator();
while(it.hasNext()) {
Title title = (Title) it.next();
SOLRTitle solrTitle = new SOLRTitle();
solrTitle.id = title.getID().toString();
solrTitle.title = title.getTitle();
solrTitles.add(solrTitle);
}
server.addBeans(solrTitles);
server.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
-----------------------------------
Querying function:
-----------------------------------
private static void queryData() {
try {
SolrQuery query = new SolrQuery();
query.setQuery( "kitten");
QueryResponse rsp = server.query( query );
List<SOLRTitle> beans = rsp.getBeans(SOLRTitle.class);
System.out.println(beans.size());
Iterator<SOLRTitle> it = beans.iterator();
while(it.hasNext()) {
SOLRTitle solrTitle = (SOLRTitle)it.next();
System.out.println(solrTitle.id);
System.out.println(solrTitle.title);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
--------------------------------------
Subhash Bhushan.
Re: SOLRJ - Searching text in all fields of a Bean
Posted by Subhash Bhushan <su...@stratalabs.in>.
Hi Savvas,
Thanks!! Was able to search using <copyField/> directive.
I was using the default example schema packaged with solr. I added the
following directive for title field and reindexed data:
*<copyField source="title" dest="text"/>*
Regards,
Subhash Bhushan.
On Fri, Oct 8, 2010 at 2:09 PM, Savvas-Andreas Moysidis <
savvas.andreas.moysidis@googlemail.com> wrote:
> Hello,
>
> What does your schema look like? Have you defined a "catch all" field and
> copy every value from all your other fields in it with a <copyField />
> directive?
>
> Cheers,
> -- Savvas
>
>
> On 8 October 2010 08:30, Subhash Bhushan <su...@stratalabs.in>wrote:
>
>> Hi,
>>
>> I have two fields in the bean class, id and title.
>> After adding the bean to SOLR, I want to search for, say "kitten", in all
>> defined fields in the bean, like this -- query.setQuery( "kitten"); --
>> But I get results only when I affix the bean field name before the search
>> text like this -- query.setQuery( "title:kitten"); --
>>
>> Same case even when I use SolrInputDocument, and add these fields.
>>
>> Can we search text in all fields of a bean, without having to specify a
>> field?
>> If we can, what am I missing in my code?
>>
>> *Code:*
>> Bean:
>> -----------------------------------
>> public class SOLRTitle {
>> @Field
>> public String id = "";
>> @Field
>> public String title = "";
>> }
>> -----------------------------------
>> Indexing function:
>> -----------------------------------
>>
>> private static void uploadData() {
>>
>> try {
>> ... // Get Titles
>> List<SOLRTitle> solrTitles = new
>> ArrayList<SOLRTitle>();
>> Iterator<Title> it = titles.iterator();
>> while(it.hasNext()) {
>> Title title = (Title) it.next();
>> SOLRTitle solrTitle = new SOLRTitle();
>> solrTitle.id = title.getID().toString();
>> solrTitle.title = title.getTitle();
>> solrTitles.add(solrTitle);
>> }
>> server.addBeans(solrTitles);
>> server.commit();
>> } catch (SolrServerException e) {
>> e.printStackTrace();
>> } catch (IOException e) {
>> e.printStackTrace();
>> }
>> }
>> -----------------------------------
>> Querying function:
>> -----------------------------------
>>
>> private static void queryData() {
>>
>> try {
>> SolrQuery query = new SolrQuery();
>> query.setQuery( "kitten");
>>
>> QueryResponse rsp = server.query( query );
>> List<SOLRTitle> beans = rsp.getBeans(SOLRTitle.class);
>> System.out.println(beans.size());
>> Iterator<SOLRTitle> it = beans.iterator();
>> while(it.hasNext()) {
>> SOLRTitle solrTitle = (SOLRTitle)it.next();
>> System.out.println(solrTitle.id);
>> System.out.println(solrTitle.title);
>> }
>> } catch (SolrServerException e) {
>> e.printStackTrace();
>> }
>> }
>> --------------------------------------
>>
>> Subhash Bhushan.
>>
>
>
Re: SOLRJ - Searching text in all fields of a Bean
Posted by Savvas-Andreas Moysidis <sa...@googlemail.com>.
Hello,
What does your schema look like? Have you defined a "catch all" field and
copy every value from all your other fields in it with a <copyField />
directive?
Cheers,
-- Savvas
On 8 October 2010 08:30, Subhash Bhushan <su...@stratalabs.in>wrote:
> Hi,
>
> I have two fields in the bean class, id and title.
> After adding the bean to SOLR, I want to search for, say "kitten", in all
> defined fields in the bean, like this -- query.setQuery( "kitten"); --
> But I get results only when I affix the bean field name before the search
> text like this -- query.setQuery( "title:kitten"); --
>
> Same case even when I use SolrInputDocument, and add these fields.
>
> Can we search text in all fields of a bean, without having to specify a
> field?
> If we can, what am I missing in my code?
>
> *Code:*
> Bean:
> -----------------------------------
> public class SOLRTitle {
> @Field
> public String id = "";
> @Field
> public String title = "";
> }
> -----------------------------------
> Indexing function:
> -----------------------------------
>
> private static void uploadData() {
>
> try {
> ... // Get Titles
> List<SOLRTitle> solrTitles = new
> ArrayList<SOLRTitle>();
> Iterator<Title> it = titles.iterator();
> while(it.hasNext()) {
> Title title = (Title) it.next();
> SOLRTitle solrTitle = new SOLRTitle();
> solrTitle.id = title.getID().toString();
> solrTitle.title = title.getTitle();
> solrTitles.add(solrTitle);
> }
> server.addBeans(solrTitles);
> server.commit();
> } catch (SolrServerException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> -----------------------------------
> Querying function:
> -----------------------------------
>
> private static void queryData() {
>
> try {
> SolrQuery query = new SolrQuery();
> query.setQuery( "kitten");
>
> QueryResponse rsp = server.query( query );
> List<SOLRTitle> beans = rsp.getBeans(SOLRTitle.class);
> System.out.println(beans.size());
> Iterator<SOLRTitle> it = beans.iterator();
> while(it.hasNext()) {
> SOLRTitle solrTitle = (SOLRTitle)it.next();
> System.out.println(solrTitle.id);
> System.out.println(solrTitle.title);
> }
> } catch (SolrServerException e) {
> e.printStackTrace();
> }
> }
> --------------------------------------
>
> Subhash Bhushan.
>
Re: SOLRJ - Searching text in all fields of a Bean
Posted by Ahmet Arslan <io...@yahoo.com>.
You can replace query.setQueryType("dismax") with query.set("defType", "dismax");
Also don't forget to request title field with fl parameter. query.addField("title");
Re: SOLRJ - Searching text in all fields of a Bean
Posted by Subhash Bhushan <su...@stratalabs.in>.
Ahmet,
I got it working to an extent.
Now:
SolrQuery query = new SolrQuery();
query.setQueryType("dismax");
query.setQuery( "kitten");
query.setParam("qf", "title");
QueryResponse rsp = server.query( query );
List<SOLRTitle> beans = rsp.getBeans(SOLRTitle.class);
System.out.println(beans.size());
Iterator<SOLRTitle> it = beans.iterator();
while(it.hasNext()) {
SOLRTitle solrTitle = (SOLRTitle)it.next();
System.out.println(solrTitle.id);
System.out.println(solrTitle.title);
}
*This code is able to find the record, and prints the ID. But fails to print
the Title.*
Whereas:
SolrQuery query = new SolrQuery();
query.setQuery( "title:kitten" );
QueryResponse rsp = server.query( query );
SolrDocumentList docs = rsp.getResults();
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while (iter.hasNext()) {
SolrDocument resultDoc = iter.next();
String title = (String) resultDoc.getFieldValue("
title");
String id = (String) resultDoc.getFieldValue("id"); //id is
the uniqueKey field
System.out.println(id);
System.out.println(title);
}
*
This query succeeds!*
What am I doing wrong in dismax params? The title field is being fetched as
Null.
Regards,
Subhash Bhushan.
On Fri, Oct 8, 2010 at 2:05 PM, Ahmet Arslan <io...@yahoo.com> wrote:
> > I have two fields in the bean class, id and title.
> > After adding the bean to SOLR, I want to search for, say
> > "kitten", in all
> > defined fields in the bean, like this -- query.setQuery(
> > "kitten"); --
> > But I get results only when I affix the bean field name
> > before the search
> > text like this -- query.setQuery( "title:kitten"); --
> >
> > Same case even when I use SolrInputDocument, and add these
> > fields.
> >
> > Can we search text in all fields of a bean, without having
> > to specify a
> > field?
>
> With dismax, you can query several fields using different boosts.
> http://wiki.apache.org/solr/DisMaxQParserPlugin
>
>
>
>
>
Re: SOLRJ - Searching text in all fields of a Bean
Posted by Ahmet Arslan <io...@yahoo.com>.
> I have two fields in the bean class, id and title.
> After adding the bean to SOLR, I want to search for, say
> "kitten", in all
> defined fields in the bean, like this -- query.setQuery(
> "kitten"); --
> But I get results only when I affix the bean field name
> before the search
> text like this -- query.setQuery( "title:kitten"); --
>
> Same case even when I use SolrInputDocument, and add these
> fields.
>
> Can we search text in all fields of a bean, without having
> to specify a
> field?
With dismax, you can query several fields using different boosts.
http://wiki.apache.org/solr/DisMaxQParserPlugin