Generating Query

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating Query

bourne71
Hi,

I am trying to build a query that looks like the following:
url:(+news +politics)^1.5 content:(+news +politics)^2.0

But I can't seems to find any reference to it. I try hardcoding it like the following:
BooleanQuery query = new BooleanQuery();
query.add(new TermQuery(new Term(field, "+news +politics")), BooleanClause.Occur.SHOULD);

But with this, the query doesn't seems to provide any response or effect. By right its suppose to boost the field of the page that contain both of the word in it.

Can anyone advise me on how to create this type of query? Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

Adriano Crestani-2
Try this:

     BooleanQuery b1 =  new BooleanQuery();
     b1.add(new TermQuery(new Term("url", "news")),
BooleanClause.Occur.MUST);
     b1.add(new TermQuery(new Term("url", "politics")),
BooleanClause.Occur.MUST);
     b1.setBoost(1.5f);

     BooleanQuery b2 =  new BooleanQuery();
     b2.add(new TermQuery(new Term("content", "news")),
BooleanClause.Occur.MUST);
     b2.add(new TermQuery(new Term("content", "politics")),
BooleanClause.Occur.MUST);
     b2.setBoost(2f);

     BooleanQuery finalQuery =  new BooleanQuery();
     finalQuery.add(b1, BooleanClause.Occur.SHOULD);
     finalQuery.add(b2, BooleanClause.Occur.SHOULD);

     System.out.println(finalQuery.toString());

On Wed, Aug 12, 2009 at 2:09 AM, bourne71 <[hidden email]> wrote:

>
> Hi,
>
> I am trying to build a query that looks like the following:
> url:(+news +politics)^1.5 content:(+news +politics)^2.0
>
> But I can't seems to find any reference to it. I try hardcoding it like the
> following:
> BooleanQuery query = new BooleanQuery();
> query.add(new TermQuery(new Term(field, "+news +politics")),
> BooleanClause.Occur.SHOULD);
>
> But with this, the query doesn't seems to provide any response or effect.
> By
> right its suppose to boost the field of the page that contain both of the
> word in it.
>
> Can anyone advise me on how to create this type of query? Thanks
> --
> View this message in context:
> http://www.nabble.com/Generating-Query-tp24931880p24931880.html
> Sent from the Lucene - Java Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

iorixxx
In reply to this post by bourne71
> I am trying to build a query that looks like the
> following:
> url:(+news +politics)^1.5 content:(+news +politics)^2.0
>
> But I can't seems to find any reference to it. I try
> hardcoding it like the
> following:
> BooleanQuery query = new BooleanQuery();
> query.add(new TermQuery(new Term(field, "+news
> +politics")),
> BooleanClause.Occur.SHOULD);

Query t1 = new TermQuery(new Term("url", "news"));
Query t2 = new TermQuery(new Term("url", "politics"));

Query t3 = new TermQuery(new Term("content", "news"));
Query t4 = new TermQuery(new Term("content", "politics"));

BooleanQuery b1 = new BooleanQuery();
b1.add(t1, BooleanClause.Occur.MUST);
b1.add(t2, BooleanClause.Occur.MUST);
b1.setBoost(1.5f);

BooleanQuery b2 = new BooleanQuery();
b2.add(t3, BooleanClause.Occur.MUST);
b2.add(t4, BooleanClause.Occur.MUST);
b2.setBoost(2.0f);
       
BooleanQuery finalQuery = new BooleanQuery();
finalQuery.add(b1,BooleanClause.Occur.SHOULD);
finalQuery.add(b2,BooleanClause.Occur.SHOULD);


     

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

bourne71
thanks for the suggestion, but unfortunately it does not work ><

Ahmet Arslan wrote
> I am trying to build a query that looks like the
> following:
> url:(+news +politics)^1.5 content:(+news +politics)^2.0
>
> But I can't seems to find any reference to it. I try
> hardcoding it like the
> following:
> BooleanQuery query = new BooleanQuery();
> query.add(new TermQuery(new Term(field, "+news
> +politics")),
> BooleanClause.Occur.SHOULD);

Query t1 = new TermQuery(new Term("url", "news"));
Query t2 = new TermQuery(new Term("url", "politics"));

Query t3 = new TermQuery(new Term("content", "news"));
Query t4 = new TermQuery(new Term("content", "politics"));

BooleanQuery b1 = new BooleanQuery();
b1.add(t1, BooleanClause.Occur.MUST);
b1.add(t2, BooleanClause.Occur.MUST);
b1.setBoost(1.5f);

BooleanQuery b2 = new BooleanQuery();
b2.add(t3, BooleanClause.Occur.MUST);
b2.add(t4, BooleanClause.Occur.MUST);
b2.setBoost(2.0f);
       
BooleanQuery finalQuery = new BooleanQuery();
finalQuery.add(b1,BooleanClause.Occur.SHOULD);
finalQuery.add(b2,BooleanClause.Occur.SHOULD);


     

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

iorixxx
> thanks for the suggestion, but unfortunately it does not
> work.

What are you trying to do? Both Adriano's and my query satisfies what you were asking for. What didn't work?


     

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

bourne71
I am trying to boost  results that have all the query in it to increase its ranking. But both the query unfortunately does not seems to effect it
Ahmet Arslan wrote
> thanks for the suggestion, but unfortunately it does not
> work.

What are you trying to do? Both Adriano's and my query satisfies what you were asking for. What didn't work?


     

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

iorixxx
> I am trying to boost  results that have all the query
> in it to increase its ranking. But both the query unfortunately does not > seems to effect it

Did you read last two messages on this thread?

http://www.nabble.com/Generating-Query-for-Multiple-Clauses-in-a-Single-Field-td24694748.html 

And do not forget to use your new similarity class in both indexing and searching. IndexSearcher.setSimilarity and IndexWriter.setSimilarity.




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

bourne71
hm...try tat...but doesn't seems to be working for me though
Ahmet Arslan wrote
> I am trying to boost  results that have all the query
> in it to increase its ranking. But both the query unfortunately does not > seems to effect it

Did you read last two messages on this thread?

http://www.nabble.com/Generating-Query-for-Multiple-Clauses-in-a-Single-Field-td24694748.html 

And do not forget to use your new similarity class in both indexing and searching. IndexSearcher.setSimilarity and IndexWriter.setSimilarity.




---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

iorixxx
> hm...try tat...but doesn't seems to be working for me though

Discarding lengthNorm didn't work for you. Very interesting. I am not sure but I think inverse document frequency causing problem to you. Probably one of query word (very common word) has high document frequency, and the docs having other query words are getting higher rank. Or one your query word (very rare word) has very small document frequency, and docs having only that word are getting higher rank.

You can override another method (idf) of Similarity to ignore idf as well as lengthNorm.

public float idf(int docFreq, int numDocs) {
    return 1.0f;
  }
public float lengthNorm(String fieldName, int numTerms) {
    return 1.0f;
  }

Ofcourse this modifications will cause some side-effects but probably you will see all of your query words in the first document. If such document exists in your index. Please let me know if this works for you.


     

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Generating Query

iorixxx
In reply to this post by bourne71
> hm...try tat...but doesn't seems to be working for me though

Discarding lengthNorm didn't work for you. Very interesting. I am not sure but I think inverse document frequency causing problem to you. Probably one of query word (very common word) has high document frequency, and the docs having other query words are getting higher rank. Or one your query word (very rare word) has very small document frequency, and docs having only that word are getting higher rank.

You can override another method (idf) of Similarity to ignore idf as well as lengthNorm.

public float idf(int docFreq, int numDocs) {
    return 1.0f;
  }
public float lengthNorm(String fieldName, int numTerms) {
    return 1.0f;
  }

Ofcourse this modifications will cause some side-effects but probably you will see all of your query words in the first document. If such document exists in your index. Can you confirm this by ANDing all of your query words and look if you are getting results? Please let me know if this works for you.


     

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]