Sameeh Harfoush
Sameeh Harfoush

Reputation: 610

Create Solr extended dismax query and use it with SolrIndexSearcher

i am trying to create an edismax query and set the query params like the defType, df, q.op...ect pragmatically. I was able to create a boolean query as below but couldn't set the query parameters. any idea how?

private List<String> getBoostedElevationObj(ResponseBuilder rb) {
    SolrQueryRequest req = rb.req;

    Query query = rb.getQuery();

    List<String> docIds = null;

    try {
        BooleanQuery docIdsBq = new BooleanQuery();                     

        TermQuery tq2 = new TermQuery(new Term("subscription", "yes"));
        docIdsBq.add(tq2, BooleanClause.Occur.MUST);                                    

        SolrIndexSearcher solrIndexSearcher = req.getSearcher();

        DocList docList = solrIndexSearcher.getDocList(query, docIdsBq, null,
                0, 5);                      

        DocIterator docIterator = docList.iterator();
        docIds = new ArrayList<String>();
        int docId;
        Document doc = null;
        while (docIterator.hasNext()) {
            docId = docIterator.nextDoc();
            doc = solrIndexSearcher.doc(docId);             
            docIds.add(doc.get(idField));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return docIds;
}

Upvotes: 0

Views: 216

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

Being that those are query parser parameters, and you are eliminating the query parser in these manually constructed queries, the responsibility for some of that functionality falls to you.

  • defType - Specifies the behavior of the query parser, and since you aren't using a query parser here, you have to construct queries appropriate to the types. If you are searching an int field, NumericRangeQuery is appropriate, etc.
  • df - This also applies to parsed queries. If you are constructing your queries manually, you need to specify the field.
  • q.op - When a clause is added to a BooleanQuery, you must also specify a BooleanClause.Occur setting, so there is no default operator.

some other common params:

  • sort - Passed into the getDocList call, third argument, in the form of a Sort
  • start, rows and filter - Passed directly into getDocList
  • fl - You can pass a list of fields to be returned into the IndexSearcher.doc call.

Also, you should keep in mind, that edismax generally produces a DisjunctionMaxQuery to join clauses, rather than a BooleanQuery, and there are significant differences in how they are scored and constructed. In the case of the BooleanQuery you've created this isn't an issue, since: A - it only has one clause, and B - it's being applied as a filter. However, probably worth keeping in mind if you have other cases in mind here in which that might be an issue.

Upvotes: 1

Related Questions