gpt4 book ai didi

java - 用于 lucene 搜索的 Rest API

转载 作者:行者123 更新时间:2023-12-02 02:19:21 25 4
gpt4 key购买 nike

我已经成功创建了一个用于 Lucene 索引和搜索的程序。现在,我需要为我的 Lucene 实现 REST API,但我遇到了一些错误。

谁能坚持告诉我如何在使用 Lucene 搜索时实现 REST API 来检索我的文档?

在包含 Lucene 代码时,我收到“HTTP 状态 500 - 内部服务器错误”。
我特此附上我的代码,其中包含 REST-API 和 Lucene 搜索。

@Path("/lucene")
public class hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello(@QueryParam("startdate") Date startdate,
@QueryParam("enddate") Date enddate,
@QueryParam("services") String services,
@QueryParam("type") String type) throws IOException {
String hello = hellosearch(startdate, enddate, services, type);
return hello;
}


private String hellosearch(Date startdate, Date enddate, String services, String type) throws IOException {

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
final String index = "purple";

IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
final IndexSearcher searcher = new IndexSearcher(reader);
final Analyzer analyzer = new StandardAnalyzer();

Date startDate = startdate;
Date endDate = enddate;
String service = services;
String types = type;

TopDocs results = searchDetails(searcher,startDate, endDate, service, types);

ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = Math.toIntExact(results.totalHits.value);
int end = numTotalHits;
String resource = "";

for (int i = 0; i < end; i++) {
Document doc = searcher.doc(hits[i].doc);
System.out.println(" service :" + doc.get("services"));
System.out.println(" type :" + doc.get("type"));
System.out.println(" message :" + doc.get("message"));
resource = resource + "services : " + doc.get("services") + "\ntype : " + doc.get("type") + "\nmessage : " + doc.get("message") + "\n";

}
return resource;
}


private TopDocs searchDetails(IndexSearcher searcher,Date startdate, Date enddate, String service, String types) throws IOException {
String field = "";
long millis = 0;

Date startDate = startdate;

Date endDate = enddate;

long startmillis = startDate.getTime();
long endmillis = endDate.getTime();
String upperdate = DateTools.dateToString(new Date(startmillis), DateTools.Resolution.SECOND);
String lowerdate = DateTools.dateToString(new Date(endmillis), DateTools.Resolution.SECOND);

TermRangeQuery query = new TermRangeQuery("date", new BytesRef(upperdate), new BytesRef(lowerdate), true, true);

String services = service;
Term term1 = new Term("services", services);
Query query1 = new WildcardQuery(term1);


String type = types;
Term term2 = new Term("type", type);
Query query2 = new FuzzyQuery(term2);

Query query3 = new MatchAllDocsQuery();

BooleanQuery booleanquery = new BooleanQuery.Builder().add(query, BooleanClause.Occur.MUST)
.add(query1, BooleanClause.Occur.MUST)
.add(query2, BooleanClause.Occur.MUST)
.add(query3, BooleanClause.Occur.MUST).build();

TopDocs results = searcher.search(booleanquery, 200);
return results;
}
}

我已经索引了一个 csv 文件,它包含日期、服务和类型。我需要从用户那里获取 startdateenddate 以及服务和类型,以便我需要检索与特定日期内给定服务和类型匹配的索引文档。

最佳答案

据我了解这个问题,更多的是关于将日期作为休息端点中的参数传递。您可以通过以下方式进行操作。

public String hello(@RequestParam("startdate") @DateTimeFormat(pattern = "dd.MM.yyyy") Date date,
... other parameters ) throws IOException {
.... other code
}

或者你可以尝试这样写。

public String hello(@RequestParam("startdate")  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localDate,
... other parameters ) throws IOException {
.... other code
}

除此之外,我建议使用 Apache Solr 或 Elastic Search,您将获得用于搜索的 Rest 端点。

关于java - 用于 lucene 搜索的 Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57285132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com