2014-01-15 22 views
2

過濾我有以下映射:如何使用方面與嵌套的文件上ElasticSearch

curl -XPUT 'http://localhost:9200/bookstore/user/_mapping' -d ' 
{ 
    "user": { 
    "properties": { 
     "user_id": { "type": "integer" }, 
     "gender": { "type": "string", "index" : "not_analyzed" }, 
     "age": { "type": "integer" }, 
     "age_bracket": { "type": "string", "index" : "not_analyzed" }, 
     "current_city": { "type": "string", "index" : "not_analyzed" }, 
     "relationship_status": { "type": "string", "index" : "not_analyzed" }, 
     "books" : { 
     "type": "nested", 
     "properties" : { 
      "b_oid": { "type": "string", "index" : "not_analyzed" }, 
      "b_name": { "type": "string", "index" : "not_analyzed" }, 
      "bc_id": { "type": "integer" }, 
      "bc_name": { "type": "string", "index" : "not_analyzed" }, 
      "bcl_name": { "type": "string", "index" : "not_analyzed" }, 
      "b_id": { "type": "integer" } 
     } 
     } 
    } 
    } 
}' 

現在,我嘗試查詢例如對於具有「性別」用戶:「男」,已經買了書某個類別「bcl_name」:「Trivia」並顯示「b_name」書名。我以某種方式無法讓它運行。

我有查詢

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{ 
    "size": 0, 
    "from": 0, 
    "query": { 
    "filtered": { 
     "query": { 
      "terms": { 
       "gender": [ 
        "Male" 
       ] 
      } 
     } 
    } 
    }, 
    "facets": { 
     "CategoryFacet": { 
      "terms": { 
       "field": "books.b_name", 
       "size": 5, 
       "shard_size": 1000, 
       "order": "count" 
      }, 
      "nested": "books", 
      "facet_filter": { 
       "terms": { 
        "books.bcl_name": [ 
         "Trivia" 
        ] 
       } 
      } 
     } 
    } 
}' 

它返回一個結果,但我不知道這是否是正確的。我查找了一些例子,並找到了這個例子(http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/)。我能夠像這樣重寫我的查詢:

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{ 
    "size": 0, 
    "from": 0, 
    "query": { 
    "filtered": { 
     "query": { 
      "terms": { 
       "gender": [ 
        "Male" 
       ] 
      } 
     }, 
     "filter": { 
      "nested": { 
       "path": "books", 
       "query": { 
        "filtered": { 
         "query": { 
          "match_all": {} 
         }, 
         "filter": { 
          "and": [ 
           { 
            "term": { 
             "books.bcl_name": "Trivia" 
            } 
           } 
          ] 
         } 
        } 
       } 
      } 
     } 
    } 
    }, 
    "facets": { 
    "CategoryFacet": { 
     "terms": { 
      "field": "books.b_name", 
      "size": 5, 
      "shard_size": 1000, 
      "order": "count" 
     }, 
     "nested": "books" 
    } 
    } 
}' 

它顯示不同的結果。

我作爲一個初學者,現在已經失去了一塊兒。有人可以給我提示如何解決這個問題嗎?提前感謝!

回答

1

第一次查詢的意思是:

  • 搜索其gender : "Male"用戶
  • 但 「CategoryFacet」 包括這樣的結果集,你得到所有 「男」 的網友gender : "Male" AND books.bcl_name : "Trivia"

計數,但您的CategoryFacet會爲您提供「男性用戶和其books.bcl_name是瑣事」的數量。

在第二個查詢中,您的「CategoryFacet」不包括額外的篩選。它只是返回確切結果集中的方面。

+0

感謝您的回覆。我仍然有點困惑,因爲我讀過查詢部分使用的過濾器不會影響分面結果。我無法實現的是,我可以在嵌套文檔構面過濾器中篩選父文檔字段。有人能給我一個這個用例的實例嗎? – Tobi

+0

實際上,查詢部分中的過濾器會影響構面。如果你不想要這個,使用搜索過濾器。我無法在新的elasticsearch指南中找到它,但這裏有一篇很好的博客文章:http://substantial.com/blog/2013/01/16/building-faceted-search-with-elasticsearch/ – Mustafa

相關問題