Skip to content

Commit

Permalink
Update API retrieval results format (#2499)
Browse files Browse the repository at this point in the history
* change format to match RunOutputWriter jsonl format

* remove whitespace
  • Loading branch information
16BitNarwhal authored May 16, 2024
1 parent f5acf4c commit a6ea614
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 103 deletions.
2 changes: 0 additions & 2 deletions src/main/frontend/pages/components/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ const Dropdown: React.FC<Props> = ({ onSelect }) => {
];
});
setCollections(generatedMap);

console.log('collections', collections);
}, []);

return (
Expand Down
17 changes: 10 additions & 7 deletions src/main/frontend/pages/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/

import React, { useEffect, useState } from 'react';
import { QueryResult } from '../../types/QueryResult';
import Dropdown from './Dropdown';

const SearchBar: React.FC = () => {
const [loading, setLoading] = useState<boolean>(false);
const [results, setResults] = useState<QueryResult[]>([]);
const [results, setResults] = useState<Array<any>>([]);
const [query, setQuery] = useState<string>('');
const [index, setIndex] = useState<string>('');

Expand All @@ -32,8 +31,8 @@ const SearchBar: React.FC = () => {
endpoint += `/search?query=${query}`;

const response = await fetch(endpoint);
const data: QueryResult[] = await response.json();
setResults(data);
const data = await response.json();
setResults(data.candidates);
} catch (error) {
console.error("Failed to fetch data: ", error);
setResults([]);
Expand Down Expand Up @@ -66,10 +65,14 @@ const SearchBar: React.FC = () => {
</div>
{loading && <p>Loading...</p>}
<ul>
{results.map((result) => (
<div className="query-card" key={result.docid}>
{results.map((result, index) => (
<div className="query-card" key={index}>
<h3>Document ID: {result.docid} <span>Score: {result.score}</span></h3>
<p>{result.content}</p>
{Object.entries(result.doc).map(([key, value]) => (
<p key={key}>
<strong>{key}:</strong> {value as React.ReactNode}
</p>
))}
</div>
))}
</ul>
Expand Down
5 changes: 0 additions & 5 deletions src/main/frontend/types/QueryResult.tsx

This file was deleted.

16 changes: 13 additions & 3 deletions src/main/java/io/anserini/server/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.anserini.server;

import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -31,15 +33,23 @@ public class Controller {
private static final String DEFAULT_COLLECTION = "msmarco-v1-passage";

@RequestMapping(method = RequestMethod.GET, path = {"/collection/{collection}/search", "/search"})
public List<QueryResult> search(@PathVariable(value = "collection", required = false) String collection,
@RequestParam("query") String query) {
public Map<String, Object> search(@PathVariable(value = "collection", required = false) String collection,
@RequestParam("query") String query,
@RequestParam(value = "hits", defaultValue = "10") int hits,
@RequestParam(value = "qid", defaultValue = "") String qid) {

if (collection == null) {
collection = DEFAULT_COLLECTION;
}

SearchService searchService = new SearchService(collection);
return searchService.search(query, 10);
List<Map<String, Object>> candidates = searchService.search(query, hits);

Map<String, Object> queryMap = new LinkedHashMap<>();
queryMap.put("query", new LinkedHashMap<>(Map.of("qid", qid, "text", query)));
queryMap.put("candidates", candidates);

return queryMap;
}

}
55 changes: 0 additions & 55 deletions src/main/java/io/anserini/server/QueryResult.java

This file was deleted.

50 changes: 23 additions & 27 deletions src/main/java/io/anserini/server/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.anserini.server;

import io.anserini.index.Constants;
import io.anserini.search.ScoredDoc;
import io.anserini.search.SimpleSearcher;
import io.anserini.util.PrebuiltIndexHandler;
Expand All @@ -24,15 +25,19 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class SearchService {

final private String indexDir;
final private float k1 = 0.9f;
final private float b = 0.4f;
private final String indexDir;
private final float k1 = 0.9f;
private final float b = 0.4f;
private final ObjectMapper mapper = new ObjectMapper();

public SearchService(String prebuiltIndex) {
PrebuiltIndexHandler handler = new PrebuiltIndexHandler(prebuiltIndex);
Expand All @@ -45,37 +50,28 @@ public SearchService(String prebuiltIndex) {
}
}

public List<QueryResult> search(String query, int hits) {
public List<Map<String, Object>> search(String query, int hits) {
try {
// index, k1, b, hits
SimpleSearcher searcher = new SimpleSearcher(indexDir);
searcher.set_bm25(k1, b);
ScoredDoc[] results = searcher.search(query, hits);
List<QueryResult> resultStrings = List.of(results).stream()
.map(result -> {
try {
String jsonString = searcher.doc_raw(result.lucene_docid);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonString);
String content;
if (jsonNode.get("contents") != null) content = jsonNode.get("contents").asText();
else if (jsonNode.get("text") != null) content = jsonNode.get("text").asText();
else if (jsonNode.get("passage") != null) content = jsonNode.get("passage").asText();
else if (jsonNode.get("body") != null) content = jsonNode.get("body").asText();
else if (jsonNode.get("segment") != null) content = jsonNode.get("segment").asText();
else content = jsonNode.toString();
return new QueryResult(result.docid, content, result.score);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
searcher.close();
return resultStrings;
List<Map<String, Object>> candidates = new ArrayList<>();
for (ScoredDoc r : results) {
String raw = r.lucene_document.get(Constants.RAW);
JsonNode rootNode = mapper.readTree(raw);
Map<String, Object> content = mapper.convertValue(rootNode, Map.class);
content.remove("docid");
Map<String, Object> candidate = new LinkedHashMap<>();
candidate.put("docid", r.docid);
candidate.put("score", r.score);
candidate.put("doc", content);
candidates.add(candidate);
}
return candidates;
} catch (Exception e) {
// Consume exception and return empty list
e.printStackTrace();
return List.of();
}
}

}
15 changes: 11 additions & 4 deletions src/test/java/io/anserini/server/ControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

public class ControllerTest {

@Test
public void testSearch() throws Exception {
Controller controller = new Controller();

List<QueryResult> results = controller.search(null, "Albert Einstein");
Map<String, Object> results = controller.search(null, "Albert Einstein", 10, "");
assertNotNull(results);
assertTrue(results.get("candidates") instanceof List);

assertEquals(results.size(), 10);
assertEquals(results.get(0).getDocid(), "3553430");
@SuppressWarnings("unchecked")
List<Map<String, Object>> candidates = (List<Map<String, Object>>) results.get("candidates");
assertEquals(10, candidates.size());
assertEquals("3553430", candidates.get(0).get("docid"));
}

@Test
public void testIndexNotFound() throws Exception {
Controller controller = new Controller();

assertThrows(RuntimeException.class, () -> {
List<QueryResult> results = controller.search("nonexistent-index", "Albert Einstein");
Map<String, Object> results = controller.search("nonexistent-index", "Albert Einstein", 10, "");
});
}

Expand Down

0 comments on commit a6ea614

Please sign in to comment.