2012-11-25 3 views
0

우리는 탄성 검색 (ES)에 관한 두 가지 유형의 문서, 즉 항목 및 슬롯 (항목은 슬롯 문서의 부모)을 사용합니다.elasitcsearch 부모 자식 문서 쿼리

curl -XPOST 'localhost:9200/items/slot/_mapping' -d @slotsdef.json 

: itemsdef.json 그런 다음 다음과 같은 정의

{ 
"mappings" : { 
    "item" : { 
     "properties" : { 
      "id" : {"type" : "long" }, 
      "name" : { 
       "type" : "string", 
       "_analyzer" : "textIndexAnalyzer" 
      }, 
      "location" : {"type" : "geo_point" }, 
     } 
    } 
}, 
"settings" : { 
    "analysis" : { 
     "analyzer" : { 

       "activityIndexAnalyzer" : { 
        "alias" : ["activityQueryAnalyzer"], 
        "type" : "custom", 
        "tokenizer" : "whitespace", 
        "filter" : ["trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"] 
       }, 
       "textIndexAnalyzer" : { 
        "type" : "custom", 
        "tokenizer" : "whitespace", 
        "filter" : ["word_delimiter_impl", "trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"] 
       }, 
       "textQueryAnalyzer" : { 
        "type" : "custom", 
        "tokenizer" : "whitespace", 
        "filter" : ["trim", "lowercase", "asciifolding", "spanish_stop"] 
       }  
     }, 
     "filter" : {   
       "spanish_stop" : { 
        "type" : "stop", 
        "ignore_case" : true, 
        "enable_position_increments" : true, 
        "stopwords_path" : "analysis/spanish-stopwords.txt" 
       }, 
       "spanish_synonym" : { 
        "type" : "synonym", 
        "synonyms_path" : "analysis/spanish-synonyms.txt" 
       }, 
       "word_delimiter_impl" : { 
        "type" : "word_delimiter", 
        "generate_word_parts" : true, 
        "generate_number_parts" : true, 
        "catenate_words" : true, 
        "catenate_numbers" : true, 
        "split_on_case_change" : false     
       }    
     } 
    } 
} 
} 

을 가지고 우리는 다음과 같은 명령을 사용하여 자식 문서 정의를 추가

curl -XPOST 'localhost:9200/items' -d @itemsdef.json 

: 우리는 다음 명령을 사용하여 인덱스를 정의 slotsdef.json의 정의는 다음과 같습니다.

,451,515,
{ 
"slot" : { 
    "_parent" : {"type" : "item"}, 
    "_routing" : { 
     "required" : true, 
     "path" : "parent_id" 
    }, 
    "properties": { 
     "id" : { "type" : "long" }, 
     "parent_id" : { "type" : "long" }, 
     "activity" : { 
      "type" : "string", 
      "_analyzer" : "activityIndexAnalyzer" 
     }, 
     "day" : { "type" : "integer" }, 
     "start" : { "type" : "integer" }, 
     "end" : { "type" : "integer" } 
    } 
} 
} 

마지막으로 우리는 다음과 같은 명령으로 대량 지수를 수행

curl -XPOST 'localhost:9200/items/_bulk' --data-binary @testbulk.json 
testbulk.json 다음 데이터 보유

: 나는 다음과 같은 쿼리를 만들려고

{"index":{"_type": "item", "_id":35}} 
{"location":[40.4,-3.6],"id":35,"name":"A Name"} 
{"index":{"_type":"slot","_id":126,"_parent":35}} 
{"id":126,"start":1330,"day":1,"end":1730,"activity":"An Activity","parent_id":35} 

을 : 검색 지정된 일 및 특정 시작 및 끝 범위 내에서 하위 (슬롯)가있는 위치까지의 특정 거리 내의 모든 항목에 대해

조건을 충족하는 슬롯이 더 많은 항목의 점수가 높아야합니다.

기존 샘플부터 시작했지만 문서는 실제로 부족하고 앞으로 나아갈 수 없습니다.

단서?

답변

0

위치를 슬롯으로 이동하지 않고도 이와 같은 작업을 효율적으로 수행 할 수있는 방법은 없을 것이라고 생각합니다. 당신이 뭔가를 할 수 있지만 일부 데이터에 대한 매우 비효율적 할 수 있습니다 기본적으로

{ 
    "query": { 
     "top_children" : { 
      "type": "blog_tag", 
      "query" : { 
       "constant_score" : { 
        "query" : { 
         ... your query for children goes here ... 
        } 
       }    
      }, 
      "score" : "sum", 
      "factor" : 5, 
      "incremental_factor" : 2 
     } 
    }, 
    "filter": { 
     "geo_distance" : { 
      "distance" : "200km", 
       "location" : { 
        "lat" : 40, 
        "lon" : -70 
       } 
      } 
     } 
    } 
} 

,하고있는이 쿼리이 무엇인지, 그것은 어린이와 당신이 필요로하는 어떤 다른 조건과 랩에 대한 범위 쿼리 또는 필터 소요 그것을 constant_score 쿼리에 넣어 모든 하위 항목의 점수가 1.0인지 확인하십시오. top_children 쿼리는 이러한 모든 자식을 수집하고 해당 점수를 부모에게 누적합니다. 그런 다음 너무 멀리있는 부모를 필터링하여 필터링합니다.

+0

어느 정도의 비정규 화 적용은 어떻게됩니까? 어쩌면 검색 가능한 모든 부모 특성을 자식으로 이동하고 부모를 자식을 그룹화하는 구조로 남겨 둘 수 있습니다. 중첩 된 문서가 더 적합할까요? –

+0

그래, 내가 대답의 시작 부분에 언급 한대로 슬롯에 도움이 될 수도 있습니다. 합리적인 성능을 얻었는지 확인하기 위해 몇 가지 테스트를 수행 한 다음 비정규 화 후에 이러한 테스트를 반복하는 것이 좋습니다. – imotov