2015-11-27 94 views
0

我有這個映射&查詢。除非我想用提及的「tagid」來過濾那些內容,否則一切正常。它返回零結果。ElasticSearch:如何爲嵌套對象創建複雜的查詢和過濾器?

我想過濾基於標籤id的內容。

{ 
    "mappings": { 
    "video": { 
     "_all": { 
     "enabled": true 
     }, 
     "properties": { 
     "title": { 
      "type": "string" 
     }, 
     "en_title": { 
      "type": "string" 
     }, 
     "tags": { 
      "type": "nested", 
      "properties": { 
      "tagname": { 
       "type": "string" 
      }, 
      "tagid": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     }, 
     "metadescription": { 
      "type": "string" 
     }, 
     "author": { 
      "type": "string" 
     }, 
     "description": { 
      "type": "string" 
     }, 
     "items": { 
      "type": "nested", 
      "properties": { 
      "item_title": { 
       "type": "string" 
      }, 
      "item_duration": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     }, 
     "isfeatured": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "image": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "contenttype": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "category": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "categoryalias": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "url": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "authorid": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "price": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "duration": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "publishdate": { 
      "type": "date", 
      "format": "yyyy-MM-dd HH:mm:ss" 
     } 
     } 
    } 
    } 
} 

,這是查詢:

{ 
    "index": "content", 
    "type": "video", 
    "body": { 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": { } 
     }, 
     "filter": { 
      "bool": { 
      "must": [ 
       { 
       "nested": { 
        "path": "tags", 
        "query": { 
        "bool": { 
         "should": [ 
         { 
          "term": { 
          "tagid": "193" 
          } 
         }, 
         { 
          "term": { 
          "tagid": "194" 
          } 
         } 
         ] 
        } 
        } 
       } 
       }, 
       { 
       "term": { 
        "categoryalias": "digilife" 
       } 
       }, 
       { 
       "term": { 
        "price": 0 
       } 
       } 
      ] 
      } 
     } 
     } 
    }, 
    "from": 0, 
    "size": 9, 
    "sort": [ 
     "_score" 
    ] 
    } 
} 

回答

0

nested filter在您的查詢是不太正確的。對於您有tagid的字段名稱,它應該是tags.tagid。完整的查詢應該是

{ 
"index": "content", 
"type": "video", 
"body": { 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "bool": { 
      "must": [{ 
      "nested": { 
       "path": "tags", 
       "query": { 
       "bool": { 
        "should": [{ 
        "term": { 
         "tags.tagid": "193" 
        } 
        }, { 
        "term": { 
         "tags.tagid": "194" 
        } 
        }] 
       } 
       } 
      } 
      }, { 
      "term": { 
       "categoryalias": "digilife" 
      } 
      }, { 
      "term": { 
       "price": 0 
      } 
      }] 
     } 
     } 
    } 
    }, 
    "from": 0, 
    "size": 9, 
    "sort": [ 
    "_score" 
    ] 
} 
} 

編輯:

下面是一個完整的工作示例,讓您開始。我爲此使用了Sense,但您可以使用cURL或您選擇的語言客戶端。

對於映射

curl -XPUT "http://localhost:9200/content" -d' 
{ 
    "mappings": { 
    "video": { 
     "_all": { 
     "enabled": true 
     }, 
     "properties": { 
     "title": { 
      "type": "string" 
     }, 
     "en_title": { 
      "type": "string" 
     }, 
     "tags": { 
      "type": "nested", 
      "properties": { 
      "tagname": { 
       "type": "string" 
      }, 
      "tagid": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     }, 
     "metadescription": { 
      "type": "string" 
     }, 
     "author": { 
      "type": "string" 
     }, 
     "description": { 
      "type": "string" 
     }, 
     "items": { 
      "type": "nested", 
      "properties": { 
      "item_title": { 
       "type": "string" 
      }, 
      "item_duration": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     }, 
     "isfeatured": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "image": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "contenttype": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "category": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "categoryalias": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "url": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "authorid": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "price": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "duration": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "publishdate": { 
      "type": "date", 
      "format": "yyyy-MM-dd HH:mm:ss" 
     } 
     } 
    } 
    } 
}' 

我們可以檢查映射預期與

curl -XGET "http://localhost:9200/content/video/_mapping" 

現在,讓我們的索引一些文件到索引

// document with id 1 
curl -XPOST "http://localhost:9200/content/video/1" -d' 
{ 
    "tags": [ 
     { 
      "tagname" : "tag 193", 
      "tagid": "193" 
     }   
    ], 
    "price": 0, 
    "categoryalias": "digilife" 
}' 

// document with id 2 
curl -XPOST "http://localhost:9200/content/video/2" -d' 
{ 
    "tags": [ 
     { 
      "tagname" : "tag 194", 
      "tagid": "194" 
     }   
    ], 
    "price": 0, 
    "categoryalias": "digilife" 
}' 

// document with id 3 
curl -XPOST "http://localhost:9200/content/video/3" -d' 
{ 
    "tags": [ 
     { 
      "tagname" : "tag 194", 
      "tagid": "194" 
     }   
    ], 
    "price": 0, 
    "categoryalias": "different category alias" 
}' 

現在,讓我們運行查詢。我刪除了查詢的多餘部分並簡化了它

curl -XGET "http://localhost:9200/content/video/_search" -d' 
{ 
    "query": { 
     "filtered": { 
     "filter": { 
      "bool": { 
       "must": [ 
        { 
        "nested": { 
         "path": "tags", 
         "query": { 
          "terms": { 
           "tags.tagid": [ 
           "193", 
           "194" 
           ] 
          } 
         } 
        } 
        }, 
        { 
        "term": { 
         "categoryalias": "digilife" 
        } 
        }, 
        { 
        "term": { 
         "price": 0 
        } 
        } 
       ] 
      } 
     } 
     } 
    }, 
    "size": 9 
}' 

只應返回帶有ID 1和2的文檔。這與結果證實

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "content", 
      "_type": "video", 
      "_id": "1", 
      "_score": 1, 
      "_source": { 
       "tags": [ 
        { 
        "tagname": "tag 193", 
        "tagid": "193" 
        } 
       ], 
       "price": 0, 
       "categoryalias": "digilife" 
      } 
     }, 
     { 
      "_index": "content", 
      "_type": "video", 
      "_id": "2", 
      "_score": 1, 
      "_source": { 
       "tags": [ 
        { 
        "tagname": "tag 194", 
        "tagid": "194" 
        } 
       ], 
       "price": 0, 
       "categoryalias": "digilife" 
      } 
     } 
     ] 
    } 
} 
+0

我剛剛用提到的映射索引我的內容。但用你編輯的查詢,我剛收到零結果。這是一個正確的映射?這些查詢和映射是否相互匹配? –