Running MLT (more like this) Request in Solrj

I use the recent Solr 4.2.1 solrj libraries.

I am trying to execute an MLT request from a java program. It works fine while I only provide small pieces in stream.body, but this view defeats my purpose.

When I try to use a ContentStream, I get no response, when I make a solr.query request, it makes another request.

It looks like the server is getting my solr.request () ok. Appreciate any pointers.

Oh and I speak with solr 3.6.1

Here is what I still have:

import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.NamedList; import org.apache.solr.client.solrj.*; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.*; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import org.apache.solr.client.solrj.request.AbstractUpdateRequest; import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest; import org.apache.solr.client.solrj.util.ClientUtils; public class SolrJSearcher { public static void main(String[] args) throws MalformedURLException, SolrServerException { HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr"); ModifiableSolrParams params = new ModifiableSolrParams(); String mltv[] = {"Big bunch of text for testing - redacted for brevity"}; String dvalues[] = {"mlt"}; String svalues[] = {"0"}; ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/mlt"); ContentStream cs = new ContentStreamBase.StringStream(mltv[0]); up.addContentStream( cs); SolrQuery theQuery = new SolrQuery();; theQuery.set("qt", dvalues); up.setParam("start", "0"); try { solr.request(up); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } QueryResponse response = solr.query(theQuery); SolrDocumentList results = response.getResults(); for (int i = 0; i < results.size(); ++i) { System.out.println(results.get(i)); } } } 
+4
source share
1 answer

As far as I know, MoreLikeThis is for finding documents similar to a document already contained in the index. If you are looking for documents similar to the input string, just insert the temporary element in your index before executing the query and delete it later.

I have successfully used the following:

 /* * Build up a MoreLikeThis query to retrieve documents * similar to the one with id originalId */ private SolrQuery buildUpMoreLikeThisQuery(String field3, String originalId) { SolrQuery query = new SolrQuery(); query.setQueryType("/" + MoreLikeThisParams.MLT); query.set(MoreLikeThisParams.MATCH_INCLUDE, true); query.set(MoreLikeThisParams.MIN_DOC_FREQ, 1); query.set(MoreLikeThisParams.MIN_TERM_FREQ, 1); query.set(MoreLikeThisParams.MIN_WORD_LEN, 7); query.set(MoreLikeThisParams.BOOST, false); query.set(MoreLikeThisParams.MAX_QUERY_TERMS, 1000); query.set(MoreLikeThisParams.SIMILARITY_FIELDS, "field1,field2"); query.setQuery("id:" + originalId); query.set("fl", "id,score"); query.addFilterQuery("field3:" + field3); int maxResults = 20; query.setRows(maxResults); return query; } 
+5
source

All Articles