4

我正在使用最近的 Solr 4.2.1 solrj 库。

我正在尝试从 java 程序执行 MLT 查询。只要我只在 stream.body 中提供小块,它就可以正常工作,但这违背了我的目的。

当我尝试使用 ContentStream 时,我没有得到响应,当我执行 solr.query 时,它会发出另一个请求。

看起来服务器正在让我的 solr.request() 正常。感谢任何指针。

哦,我正在和一个 solr 3.6.1

这是我到目前为止所拥有的:

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

1 回答 1

5

据我所知,MoreLikeThis 旨在查找与索引中已有文档相似的文档。如果您正在搜索类似于输入字符串的文档,则只需在执行查询之前在索引中插入一个临时项目,然后将其删除。

我一直在成功使用以下内容:

/*
 * 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;
}
于 2013-04-20T07:13:47.593 回答