0
votes

is it possible to create a searchQuery where all results gets the same score ? example: I have some document types i'd like to search in, and i'd like the match on the document types to not have any effect on the score.

(nodeTypeAlias: newsItem nodeTypeAlias: activity) this Query give me all the items which has newsItem or activity as nodeTypeAlias, but the problem is that activity Items has a score of 2,7581 and newsItem items has a score of 0,1061. Which gives the activity items a headstart, and that is not wanted.

Ived tried to boost with 0, using lucene.net, but when passed to the QueryParser it converts it to ^.0, and lucene breaks on such query.

So question is: How can i get a field search to not effect the score, but other fields should (these fields i actually search in with the searchQuery).

1

1 Answers

0
votes

Wrap the query, or subquery, in a ConstantScoreQuery, like:

Query constantQuery = new ConstantScoreQuery(nodeTypeAliasBQ);
booleanQuery.add(new BooleanClause(constantQuery, BooleanClause.Occur.MUST));
booleanQuery.add(new BooleanClause(someOtherQuery, BooleanClause.Occur.SHOULD));

Prior to 3.1, ConstantScoreQuery can only be used with a Filter, rather than a Query. You can do this a couple of ways.

  1. Wrap the query in a QueryWrapperFilter:

    Query constantQuery = new ConstantScoreQuery(new QueryWrapperFilter(nodeTypeBQ))
    

    Fun fact: There is still code to support/optimize this pattern, noted in source with:

    Fix outdated usage pattern from Lucene 2.x/early-3.x: because ConstantScoreQuery only accepted filters, QueryWrapperFilter was used to wrap queries.

  2. Use a TermsFilter:

    Filter nodeTypeFilter = new TermsFilter();
    nodeTypeFilter.addTerm(new Term("nodeTypeAlias","newsItem"));
    nodeTypeFilter.addTerm(new Term("nodeTypeAlias","activity"));
    Query constantQuery = new ConstantScoreQuery(nodeTypeFilter)