2017-11-12 19 views
0

이 질문에 대한보다 유익한 제목을 제안하십시오.목록에 기반한 노드 일치

제공되는 재료 배열을 기반으로 한 레시피와 일치 시키려합니다. 배열 ['tomato', celery]을 제공하면 이름에 '토마토'가 포함되어 있고 이름에 '셀러리'가 포함 된 성분이있는 모든 요리법과 일치 시키려합니다.

오늘은 다음과 같은 쿼리를 사용

이 작동
MATCH (recipe:Recipe) 
WHERE ALL(
    ingredient IN ['tomato', 'celery'] 
    WHERE (recipe)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: ingredient}) 
) 
RETURN recipe 

는, 성분 이름이 정확히 일치 제공을,하지만 난 성분명가 전달 된 용어를 들어 있으므로 그것을 한 일치하고자하는 (즉, 'Yellow Tomatoes'는 '토마토'와 일치 함)이 검색어에 CONTAIN 키워드를 통합 할 방법이 없습니다.

내가 할 수있는 일이 가능합니까?

답변

1

나는이 쿼리를 표현하는 가장 컴팩트 한 방법은 pattern comprehensionlist comprehension을 사용 믿는다

WITH ['tomato', 'celery'] AS requiredIngredients 
MATCH (recipe:Recipe) 
WITH 
    recipe, 
    size([ 
    (recipe)-[:CONTAINS_INGREDIENT]->(ingredient:Ingredient) 
    WHERE size([requiredIngredient IN requiredIngredients WHERE ingredient.name CONTAINS requiredIngredient]) > 0 
    | ingredient] 
    ) AS containedRequiredIngredients, 
    size(requiredIngredients) AS allRequiredIngredients 
WHERE containedRequiredIngredients = allRequiredIngredients 
RETURN recipe 

아이디어는 (필요한 성분 (allRequiredIngredients)와 조리법에 포함 된 사람을 계산하는 것입니다 containedRequiredIngredients). 두 값이 같으면 래서 피가 반환됩니다.

그것은이 예제를 테스트 할 수 있습니다 :

CREATE 
    (r1:Recipe), 
    (r1)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: '50g celery (chopped)'}), 
    (r1)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: 'some tomato'}) 
+0

감사합니다,이 몇 가지 닌자 물건이다, 나는 그것을 실험 것입니다! 당신은 쿼리 문자열을 프로그래밍 방식으로 (내 경우에는 자바 스크립트에서) 구성하면이 문제에 대한 해결책이 더 우아 할 수 있다고 생각합니까? – Amygdaloideum

+0

또한이 쿼리를 실행할 수 없습니다 "잘못된 입력 'W': 예상 공백, 주석, 관계 패턴" – Amygdaloideum

+0

쿼리가 내 Neo4j 브라우저에서 제대로 실행됩니다. 특수 문자를 피하는 데 문제가있을 수 있습니까? –