【问题标题】:Neo4j data-model of documents, keywords, and word stems for searching用于搜索的文档、关键字和词干的 Neo4j 数据模型
【发布时间】:2018-02-22 16:11:51
【问题描述】:

我的目标是使用 neo4j 对文档进行两种不同的搜索。我将使用食谱(文档)作为我的示例。 假设我手头有一份配料(关键词)列表(牛奶、黄油、面粉、盐、糖、鸡蛋……),我的数据库中有一些食谱,每个食谱都附有配料。我想输入我的列表并得到两个不同的结果。一个是最接近包含我输入的所有成分的食谱。第二种是包含我所有成分的食谱组合。

给定:牛奶、黄油、面粉、盐、糖、鸡蛋

第一种情况的搜索结果可能是:

1.)糖饼干

2.)黄油曲奇

第二个结果可能是:

1.)扁面包和 Gogel-Mogel

我正在阅读要插入 neo4j 的食谱,然后从每个食谱顶部的成分列表中提取成分,然后从食谱说明中提取成分。我想以不同的方式称重,可能是 60/40 以支持成分列表。

我还想把每种成分都删掉,以防人们输入相似的词。

我正在努力在 neo4j 中提出一个好的数据模型。我计划让用户输入英文成分,我会在后台停止它们,并使用它进行搜索。

我的第一个想法是: 这对我来说很直观,但要找到所有食谱需要很多时间。

接下来可能是这样的:

直接从茎中获取食谱,但我需要在关系中传递食谱 ID(对吗?)以获取实际成分。

第三,也许像这样组合它们? 但是有很多重复。

这里还有一些 CYPHER 语句来创建第一个想法:

//Create 4 recipes
create (r1:Recipe {rid:'1', title:'Sugar cookies'}), (r2:Recipe {rid:'2', title:'Butter cookies'}), 
(r3:Recipe {rid:'3', title:'Flat bread'}), (r4:Recipe {rid:'4', title:'Gogel-Mogel'}) 

//Adding some ingredients
merge (i1:Ingredient {ingredient:"salted butter"})
merge (i2:Ingredient {ingredient:"white sugar"})
merge (i3:Ingredient {ingredient:"brown sugar"})
merge (i4:Ingredient {ingredient:"all purpose flour"})
merge (i5:Ingredient {ingredient:"iodized salt"})
merge (i6:Ingredient {ingredient:"eggs"})
merge (i7:Ingredient {ingredient:"milk"})
merge (i8:Ingredient {ingredient:"powdered sugar"})
merge (i9:Ingredient {ingredient:"wheat flour"})
merge (i10:Ingredient {ingredient:"bananas"})
merge (i11:Ingredient {ingredient:"chocolate chips"})
merge (i12:Ingredient {ingredient:"raisins"})
merge (i13:Ingredient {ingredient:"unsalted butter"})
merge (i14:Ingredient {ingredient:"wheat flour"})
merge (i15:Ingredient {ingredient:"himalayan salt"})
merge (i16:Ingredient {ingredient:"chocolate bars"})
merge (i17:Ingredient {ingredient:"vanilla flavoring"})
merge (i18:Ingredient {ingredient:"vanilla"})

//Stems added to each ingredient
merge (i1)<-[:STEM_OF]-(s1:Stem {stem:"butter"})
merge (i2)<-[:STEM_OF]-(s2:Stem {stem:"sugar"})
merge (i3)<-[:STEM_OF]-(s2)
merge (i4)<-[:STEM_OF]-(s4:Stem {stem:"flour"})
merge (i5)<-[:STEM_OF]-(s5:Stem {stem:"salt"})
merge (i6)<-[:STEM_OF]-(s6:Stem {stem:"egg"})
merge (i7)<-[:STEM_OF]-(s7:Stem {stem:"milk"})
merge (i8)<-[:STEM_OF]-(s2)
merge (i9)<-[:STEM_OF]-(s4)
merge (i10)<-[:STEM_OF]-(s10:Stem {stem:"banana"})

merge (i11)<-[:STEM_OF]-(s11:Stem {stem:"chocolate"})
merge (i12)<-[:STEM_OF]-(s12:Stem {stem:"raisin"})
merge (i13)<-[:STEM_OF]-(s1)
merge (i14)<-[:STEM_OF]-(s4)
merge (i15)<-[:STEM_OF]-(s5)
merge (i16)<-[:STEM_OF]-(s11)
merge (i17)<-[:STEM_OF]-(s13:Stem {stem:"vanilla"})
merge (i18)<-[:STEM_OF]-(s13)


create (r1)<-[:INGREDIENTS_LIST{weight:.7}]-(i1)
create (r1)<-[:INGREDIENTS_LIST{weight:.6}]-(i2)    
create (r1)<-[:INGREDIENTS_LIST{weight:.5}]-(i4)
create (r1)<-[:INGREDIENTS_LIST{weight:.4}]-(i5)
create (r1)<-[:INGREDIENTS_LIST{weight:.4}]-(i6)
create (r1)<-[:INGREDIENTS_LIST{weight:.2}]-(i7)
create (r1)<-[:INGREDIENTS_LIST{weight:.1}]-(i18)

create (r2)<-[:INGREDIENTS_LIST{weight:.7}]-(i1)
create (r2)<-[:INGREDIENTS_LIST{weight:.6}]-(i3)    
create (r2)<-[:INGREDIENTS_LIST{weight:.5}]-(i4)
create (r2)<-[:INGREDIENTS_LIST{weight:.4}]-(i5)
create (r2)<-[:INGREDIENTS_LIST{weight:.3}]-(i6)
create (r2)<-[:INGREDIENTS_LIST{weight:.2}]-(i7)
create (r2)<-[:INGREDIENTS_LIST{weight:.1}]-(i18)

create (r3)<-[:INGREDIENTS_LIST{weight:.7}]-(i1)
create (r3)<-[:INGREDIENTS_LIST{weight:.6}]-(i5)
create (r3)<-[:INGREDIENTS_LIST{weight:.5}]-(i7)
create (r3)<-[:INGREDIENTS_LIST{weight:.4}]-(i9)

create (r4)<-[:INGREDIENTS_LIST{weight:.6}]-(i2)
create (r4)<-[:INGREDIENTS_LIST{weight:.5}]-(i6)



create (r1)<-[:INGREDIENTS_INSTR{weight:.2}]-(i1)
create (r1)<-[:INGREDIENTS_INSTR{weight:.2}]-(i2)   
create (r1)<-[:INGREDIENTS_INSTR{weight:.2}]-(i4)
create (r1)<-[:INGREDIENTS_INSTR{weight:.2}]-(i5)
create (r1)<-[:INGREDIENTS_INSTR{weight:.1}]-(i6)
create (r1)<-[:INGREDIENTS_INSTR{weight:.1}]-(i7)


create (r2)<-[:INGREDIENTS_INSTR{weight:.3}]-(i1)
create (r2)<-[:INGREDIENTS_INSTR{weight:.2}]-(i3)   
create (r2)<-[:INGREDIENTS_INSTR{weight:.2}]-(i4)
create (r2)<-[:INGREDIENTS_INSTR{weight:.2}]-(i5)
create (r2)<-[:INGREDIENTS_INSTR{weight:.2}]-(i6)
create (r2)<-[:INGREDIENTS_INSTR{weight:.1}]-(i7)


create (r3)<-[:INGREDIENTS_INSTR{weight:.3}]-(i1)
create (r3)<-[:INGREDIENTS_INSTR{weight:.3}]-(i5)
create (r3)<-[:INGREDIENTS_INSTR{weight:.1}]-(i7)
create (r3)<-[:INGREDIENTS_INSTR{weight:.1}]-(i9)

create (r4)<-[:INGREDIENTS_INSTR{weight:.3}]-(i2)
create (r4)<-[:INGREDIENTS_INSTR{weight:.3}]-(i6)

以及带有上述语句的 neo4j 控制台的链接: http://console.neo4j.org/?id=3o8y44

neo4j 有多关心多重关系?另外,我可以做一种单一的成分,但是我如何组合一个查询来获取给定一种以上成分的食谱?

编辑: 谢谢迈克尔!这让我更进一步。我能够扩展您对此的回答:

WITH split("egg, sugar, chocolate, milk, flour, salt",", ") as terms  UNWIND 
terms as term  MATCH (stem:Stem {stem:term})-[:STEM_OF]->
(ingredient:Ingredient)-[lst:INGREDIENTS_LIST]->(r:Recipe)  WITH r, 
size(terms) - count(distinct stem) as notCovered,  sum(lst.weight) as weight, 
collect(distinct stem.stem) as matched  RETURN r , notCovered,matched, weight 
ORDER BY notCovered ASC, weight DESC

并获得了匹配的成分列表和重量。如何更改查询以显示 :INGREDIENTS_INSTR 关系的权重,以便我可以同时使用两个权重进行计算? [lst:INGREDIENTS_LIST|INGREDIENTS_INSTR] 不是我想要的。

编辑:

这似乎有效,对吗?

WITH split("egg, sugar, chocolate, milk, flour, salt",", ") as terms   UNWIND 
terms as term   MATCH (stem:Stem {stem:term})-[:STEM_OF]->
(ingredient:Ingredient)-[lstl:INGREDIENTS_LIST]->(r:Recipe)<-
[lsti:INGREDIENTS_INSTR]-(ingredient:Ingredient) WITH r, size(terms) - 
count(distinct stem) as notCovered,  sum(lsti.weight) as wi, sum(lstl.weight) 
as wl, collect(distinct stem.stem) as matched   RETURN r , 
notCovered,matched, wl+wi ORDER BY notCovered ASC, wl+wi DESC

另外,您能帮忙解答第二个问题吗?在给定成分列表的情况下,将返回包含给定成分的食谱组合。再次感谢!

【问题讨论】:

    标签: neo4j data-modeling keyword-search


    【解决方案1】:

    我会选择你的版本 1)。

    不用担心额外的啤酒花。 您可以将有关数量/重量的信息放在配方和实际成分之间的关​​系上。

    你可以有多个关系。

    这是一个示例查询,它不适用于您的数据集,因为您没有包含所有成分的配方:

    WITH split("milk, butter, flour, salt, sugar, eggs",", ") as terms 
    UNWIND terms as term 
    MATCH (stem:Stem {stem:term})-[:STEM_OF]->(ingredient:Ingredient)-->(r:Recipe) 
    WITH r, size(terms) - count(distinct stem) as notCovered 
    RETURN r ORDER BY notCovered ASC LIMIT 2
    
    +-----------------------------------------+
    | r                                       |
    +-----------------------------------------+
    | Node[0]{rid:"1",title:"Sugar cookies"}  |
    | Node[1]{rid:"2",title:"Butter cookies"} |
    +-----------------------------------------+
    2 rows
    

    以下将是针对大型数据集的优化:

    对于查询,您将首先找到所有成分,然后 菜谱附上最挑剔的菜谱(度数最低的菜谱)。

    然后对照每个食谱检查剩余成分。

    WITH split("milk, butter, flour, salt, sugar, eggs",", ") as terms 
    MATCH (stem:Stem) WHERE stem.stem IN terms
    // highest selective stem first
    WITH stem, terms ORDER BY size((stem)-[:STEM_OF]->()) ASC
    WITH terms, collect(stem) as stems
    WITH head(stems) first, tail(stems) as rest, terms
    MATCH (first)-[:STEM_OF]->(ingredient:Ingredient)-->(r:Recipe) 
    WHERE size[other IN rest WHERE (other)-[:STEM_OF]->(:Ingredient)-->(r)] as covered
    WITH r, size(terms) - 1 - covered as notCovered 
    RETURN r ORDER BY notCovered ASC LIMIT 2
    

    【讨论】:

    • 最后是否缺少部分答案?在冒号之后?
    • 编辑第一季度,稍后再做第二季度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2016-08-25
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多