2016-06-23 2 views
0

나는 elasticsearch 쿼리 언어를 해킹하려고 시도하고 있으며, 지금까지는별로 잘하고 있지 않습니다.태그에 대한 ElasticSearch 쿼리

내 문서에는 다음과 같은 매핑이 있습니다.

{ 
    "mappings": { 
     "jsondoc": { 
      "properties": { 
       "header" : { 
        "type" : "nested", 
        "properties" : { 
         "plainText" : { "type" : "string" }, 
         "title" : { "type" : "string" }, 
         "year" : { "type" : "string" }, 
         "pages" : { "type" : "string" } 
        } 
       }, 
       "sentences": { 
        "type": "nested", 
        "properties": { 
         "id": { "type": "integer" }, 
         "text": { "type": "string" }, 
         "tokens": { "type": "nested" }, 
         "rhetoricalClass": { "type": "string" }, 
         "babelSynsetsOcc": { 
          "type": "nested", 
          "properties" : { 
           "id" : { "type" : "integer" }, 
           "text" : { "type" : "string" }, 
           "synsetID" : { "type" : "string" } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

주로 pdf 문서를 참조하는 JSON 파일과 유사합니다.

나는 집계를 사용하여 쿼리를 작성하려고 노력 해왔고 지금까지 훌륭하게 진행되고 있습니다. 그룹화 시점까지 (집계) rhetoricalClass, 총 반복 수는 babelSynsetsOcc.synsetID입니다. 심지어 전체 쿼리 결과를 그룹화하여도 같은 쿼리 일지라도 header.year

그러나 지금은 용어가 포함 된 문서를 필터링하고 동일한 쿼리를 수행하는 데 어려움을 겪고 있습니다.

rhetoricalClass으로 그룹화하고 header.plainText 필드에 ["Computational", "Compositional", "Semantics"]이 포함 된 문서 만 고려하면 어떻게 할 수 있습니까? 나는 equal 대신 contain을 의미합니다!

난 그냥 표준 구조화 된 쿼리입니다

SELECT count(sentences.babelSynsetsOcc.synsetID) 
FROM jsondoc 
WHERE header.plainText like '%Computational%' OR header.plainText like '%Compositional%' OR header.plainText like '%Sematics%' 
GROUP BY sentences.rhetoricalClass 

답변

1

WHERE 조항으로는 비슷한 것 SQL로 대략적인 번역을, 그래서 그들은 Elasticsearch에서 쿼리에 번역한다면.

GROUP BYHAVING은 Elasticsearch의 DSL에서 집계로 느슨하게 변환됩니다. count, minmaxsum과 같은 기능은 GROUP BY의 기능이며 따라서 집계입니다.

nested 개체를 사용해야한다는 사실이 필요할 수도 있지만 추가 한 레이어는 접촉하는 각 파트에 추가 레이어를 추가합니다. 이들 nested 객체가 이 아니고 어레이가 아닌 경우 nested을 사용하지 마십시오. 이 경우 object을 사용하십시오. 아마 당신의 쿼리를 번역 보는 것

: 또는

{ 
    "query": { 
    "nested": { 
     "path": "header", 
     "query": { 
     "bool": { 
      "should": [ 
      { 
       "match": { 
       "header.plainText" : "Computational" 
       } 
      }, 
      { 
       "match": { 
       "header.plainText" : "Compositional" 
       } 
      }, 
      { 
       "match": { 
       "header.plainText" : "Semantics" 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

, 좀 덜 명백한 의도의 인이로 다시 작성할 수 있습니다 :

{ 
    "query": { 
    "nested": { 
     "path": "header", 
     "query": { 
     "match": { 
      "header.plainText": "Computational Compositional Semantics" 
     } 
     } 
    } 
    } 
} 

집계는 것 그렇다면 :

{ 
    "aggs": { 
    "nested_sentences": { 
     "nested": { 
     "path": "sentences" 
     }, 
     "group_by_rhetorical_class": { 
     "terms": { 
      "field": "sentences.rhetoricalClass", 
      "size": 10 
     }, 
     "aggs": { 
      "nested_babel": { 
      "path": "sentences.babelSynsetsOcc" 
      }, 
      "aggs": { 
      "count_synset_id": { 
       "count": { 
       "field": "sentences.babelSynsetsOcc.synsetID" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

이제는 결합하여 히트 곡을 버리면 (j 그 결과는 다음과 같습니다.

{ 
    "size": 0, 
    "query": { 
    "nested": { 
     "path": "header", 
     "query": { 
     "match": { 
      "header.plainText": "Computational Compositional Semantics" 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "nested_sentences": { 
     "nested": { 
     "path": "sentences" 
     }, 
     "group_by_rhetorical_class": { 
     "terms": { 
      "field": "sentences.rhetoricalClass", 
      "size": 10 
     }, 
     "aggs": { 
      "nested_babel": { 
      "path": "sentences.babelSynsetsOcc" 
      }, 
      "aggs": { 
      "count_synset_id": { 
       "count": { 
       "field": "sentences.babelSynsetsOcc.synsetID" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

예, 당신은 완전히 옳았습니다. 문서를 읽는 데 더 많은 노력을 기울여야하지만 공식적인 문서를 읽는 것은 매우 고통스러운 일입니다. 내 쿼리에서 누락 된 유일한 것은 중첩 된 필터였습니다. 어떻게 놓칠 수 있었는지 나는 알지 못합니다. 어쨌든, 당신의 공헌에 대해 대단히 감사합니다. – Mayhem