如何在solrJ中使用查询语法?

时间:2019-07-01 18:19:42

标签: sorting solr lucene solrj dismax

我需要在solrJ中实现此查询

&sort=query($manu_sort,0) desc, query($manu_service_sort,0) desc, query($seller_sort,0) desc, _s_name asc, sku asc &manu_sort=ManufacturerName:"Service Champ" &manu_service_sort=ManufacturerName:"SERVICE CHAMP" &seller_sort=BestSeller:true

当我直接添加到我的代码中时

        customSort = "&sort=query($manu_sort,0) desc, query($manu_service_sort,0) desc, query($seller_sort,0) desc, _s_name asc, sku asc &manu_sort=ManufacturerName:"Service Champ" &manu_service_sort=ManufacturerName:"SERVICE CHAMP" &seller_sort=BestSeller:true"; 
        queryQ = queryQ+customSort;
        solrQuery.setQuery(queryQ);
        QueryResponse e = this.provider.get(this).query(solrQuery, METHOD.POST);

这会引发类似

的错误
 no field name specified in query and no default specified via 'df' param

有什么可以帮助的吗?

1 个答案:

答案 0 :(得分:0)

我无法获得您的查询...但是可以建议一个示例代码,可能会对您有所帮助。

  String solrUrl = "http://localhost:8983/solr/";
  HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(solrUrl).build();
  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.set("defType","edismax");
  //query.set("qf","file_name^10 content_text^1");
  query.addFilterQuery("name : abcdef");

  //sorting & direction
  query.addSort("Name_of_field_For_sorting", SolrQuery.ORDER.desc);

  // set the debug 
  //query.set("debug",  "true");

  //for faceting
  //query.setFacet(true);
  //query.addFacetField("name_of_the_Field")
  //query.setFacetLimit(10);

  //set the fields
  query.setFields(new String[] {"id", "name"});

  // for highlighting 
  query.setHighlight(true);
  query.addHighlightField("Name_of_field_to_be_Highlighted");
  query.addHighlightField("content_text");
  query.setHighlightSimplePre("<hlt>");
  query.setHighlightSimplePost("</hlt>");
  query.setHighlight(true).setHighlightSnippets(2);
  query.setHighlightFragsize(150);

  query.setStart(0);
  query.setRows(10);

  QueryResponse response = httpSolrClient.query(query);
  SolrDocumentList results = response.getResults();
  //iterate the results
  for (int i = 0; i < results.size(); ++i) {
    System.out.println(results.get(i));
  }

另一种尝试可能如下。

SolrQuery query = new SolrQuery();   
String nested = "";
nested += "Field_name:";
nested += "Field_Value";
nested += " AND _query_:\"{!dismax qf=Field_name1=";
nested += "Field_Value1";
nested += "}\"";
query.setQuery(nested);
String fieldList = "id,name,category";
query.set("fl", fieldList);
QueryResponse response = httpSolrClient.query(query);