web search done right
Use all nodes to compute the inbound vector (not just top harmonic centrality ones). This makes the similarity between sites more accurate which improves explore and site_rankings
Mikkel Denker23b1fe732415
diff --git a/core/src/intmap.rs b/core/src/intmap.rsindex 9946259a..9d24af28 100644--- a/core/src/intmap.rs+++ b/core/src/intmap.rs@@ -193,6 +193,10 @@ impl<K: Key> IntSet<K> {pub fn contains(&self, item: &K) -> bool {self.map.contains_key(item)}++ pub fn into_iter(self) -> impl Iterator<Item = K> {+ self.map.into_iter().map(|(k, _)| k)+ }}impl<K: Key> std::iter::FromIterator<K> for IntSet<K> {diff --git a/core/src/ranking/centrality_store.rs b/core/src/ranking/centrality_store.rsindex d86021f4..146d8e51 100644--- a/core/src/ranking/centrality_store.rs+++ b/core/src/ranking/centrality_store.rs@@ -140,7 +140,7 @@ impl CentralityStore {let mut store = CentralityStore::open(output_path.as_ref());debug!("Begin inbound similarity index construction");- store.inbound_similarity = InboundSimilarity::build(graph, &store.harmonic);+ store.inbound_similarity = InboundSimilarity::build(graph);store.flush();storediff --git a/core/src/ranking/inbound_similarity.rs b/core/src/ranking/inbound_similarity.rsindex 7172a908..d5465f95 100644--- a/core/src/ranking/inbound_similarity.rs+++ b/core/src/ranking/inbound_similarity.rs@@ -15,8 +15,6 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.use std::{- cmp::Ordering,- collections::BinaryHeap,fs::File,io::{BufReader, BufWriter, Read},path::Path,@@ -33,8 +31,7 @@ use crate::{Result,};-use super::{bitvec_similarity, centrality_store::HarmonicCentralityStore};-const DEFAULT_NUM_TOP_HARMONIC_CENTRALITY_NODES: usize = 1_000_000;+use super::bitvec_similarity;pub struct Scorer {liked: Vec<NodeScorer>,@@ -120,79 +117,22 @@ impl Scorer {}}-#[derive(Clone, Debug)]-struct ScoredNode {- node: NodeID,- score: f64,-}--impl PartialOrd for ScoredNode {- fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {- self.score.partial_cmp(&other.score)- }-}--impl PartialEq for ScoredNode {- fn eq(&self, other: &Self) -> bool {- self.score == other.score- }-}--impl Ord for ScoredNode {- fn cmp(&self, other: &Self) -> std::cmp::Ordering {- self.partial_cmp(other).unwrap_or(Ordering::Equal)- }-}--impl Eq for ScoredNode {}-#[derive(Serialize, Deserialize, Default)]pub struct InboundSimilarity {vectors: Arc<IntMap<NodeID, bitvec_similarity::BitVec>>,}impl InboundSimilarity {- pub fn build(graph: &Webgraph, harmonic: &HarmonicCentralityStore) -> Self {- Self::build_with_threshold(graph, harmonic, DEFAULT_NUM_TOP_HARMONIC_CENTRALITY_NODES)- }-- fn build_with_threshold(- graph: &Webgraph,- harmonic: &HarmonicCentralityStore,- num_top_nodes: usize,- ) -> Self {+ pub fn build(graph: &Webgraph) -> Self {let mut vectors = IntMap::new();- let mut top_nodes: BinaryHeap<ScoredNode> = BinaryHeap::with_capacity(num_top_nodes);-- for (node, centrality) in harmonic.iter() {- let scored_node = ScoredNode {- node,- score: centrality,- };-- if top_nodes.len() >= num_top_nodes {- if let Some(mut worst) = top_nodes.peek_mut() {- if worst.score < scored_node.score {- *worst = scored_node.clone();- }- }- } else {- top_nodes.push(scored_node);- }- }-- let top_nodes: IntSet<NodeID> = top_nodes.into_iter().map(|n| n.node).collect();-- let adjacency: DashMap<NodeID, Vec<NodeID>> = DashMap::new();+ let adjacency: DashMap<NodeID, IntSet<NodeID>> = DashMap::new();graph.par_edges().for_each(|edge| {- if top_nodes.contains(&edge.from) {- adjacency- .entry(edge.to)- .or_insert_with(Vec::new)- .push(edge.from);- }+ adjacency+ .entry(edge.to)+ .or_insert_with(IntSet::new)+ .insert(edge.from);});for (node_id, inbound) in adjacency {@@ -274,11 +214,10 @@ mod tests {use crate::{gen_temp_path,index::Index,- kv::rocksdb_store::RocksDbStore,rand_words,ranking::centrality_store::CentralityStore,searcher::{LocalSearcher, SearchQuery},- webgraph::{centrality::harmonic::HarmonicCentrality, Node, WebgraphBuilder},+ webgraph::{Node, WebgraphBuilder},webpage::{Html, Webpage},};@@ -300,16 +239,7 @@ mod tests {graph.commit();- let harmonic = HarmonicCentrality::calculate(&graph);-- let harmonic_centrality_store = RocksDbStore::open(crate::gen_temp_path());- for (node, centrality) in harmonic.iter() {- harmonic_centrality_store.insert(*node, centrality);- }- harmonic_centrality_store.flush();-- let inbound =- InboundSimilarity::build_with_threshold(&graph, &harmonic_centrality_store, 1000);+ let inbound = InboundSimilarity::build(&graph);let scorer = inbound.scorer(&[Node::from("b.com").id()], &[], false);let e = Node::from("e.com").id();@@ -329,16 +259,7 @@ mod tests {graph.commit();- let harmonic = HarmonicCentrality::calculate(&graph);-- let harmonic_centrality_store = RocksDbStore::open(crate::gen_temp_path());- for (node, centrality) in harmonic.iter() {- harmonic_centrality_store.insert(*node, centrality);- }- harmonic_centrality_store.flush();-- let inbound =- InboundSimilarity::build_with_threshold(&graph, &harmonic_centrality_store, 1000);+ let inbound = InboundSimilarity::build(&graph);let mut centrality_store = CentralityStore::build(&graph, gen_temp_path());centrality_store.inbound_similarity = inbound;