2015-05-03 138 views
0
{ 
    path: '/xyz.com/electronics/1', 
    body: { 
     category: 'mobiles', 
     product: 'apple iphone 6', 
     brand: 'apple', 
     mobile_name: 'iphone 6', 
     price: 55000, 
     os: undefined, 
     memory: 16, 
     ram: 2, 
     3g: 1, 
     wifi: 1, 
     simtype: 1, 
     screen: 5.1 
    }, 
    query: {} 
} 

以上給出的是我的彈性搜索索引記錄。我想就可以應用多個過濾取決於哪些用戶請求 爲2000-5000之間的價格恩範圍,無線網絡值1,屏幕5吋以上等如何在多個過濾器的彈性搜索中應用分面搜索?

我嘗試這樣做:

client.search({ 
    index: "xyz.com", 
    type: 'electronics', 
    body: { 
     "query": { 
      "query_string": { 
       "query": "mobiles" 
      } 
     }, 
     "facets": { 
      "format": { 
       "terms": { 
        "field": "brand" 
       } 
      } 
     } 
    } 
}) 

我怎樣才能爲這些過濾器編寫一個通用查詢?

+1

代替使用QUERY_STRING的,只需使用一個[過濾](http://www.elastic.co/guide/en/ elasticsearch/reference/1.5/query-dsl-filtered-query.html)查詢並添加您需要的過濾器(例如, 2000-2500之間的價格=> [範圍](http://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-range-filter.html)過濾器等) – ThomasC

回答

0

正如Thomas所建議的那樣,您可以使用過濾後的查詢。

考慮這四個文件(指數晶面,類型DOC):

PUT facets/doc/1 
{ 
    "name":"doc1", 
    "user":"archer", 
    "tag":"elasticsearch" 
} 
PUT facets/doc/2 
{ 
    "name":"doc2", 
    "user":"betty", 
    "tag":["solr"] 
} 
PUT facets/doc/3 
{ 
    "name":"doc3", 
    "user":"cyril", 
    "tag":["elasticsearch", "solr"] 
} 
PUT facets/doc/4 
{ 
    "name":"doc4", 
    "user":"lena", 
    "tag":["java","solr"] 
} 

你可以使用此聚集查詢標籤和用戶面(注意:facet queries are deprecated):

GET facets/_search 
{ 
"aggs": { 
    "agg_user": { 
     "terms": { 
     "field": "user" 
     } 
    }, 
    "agg_tag": { 
     "terms": { 
     "field": "tag" 
     } 
    } 
    } 
} 

然後你可以filter by a specific user and a specific tag

GET facets/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      {"term": { "tag": "solr" }}, 
      {"term": { "user": "betty" }} 
      ] 
     } 
     } 
    } 
    }, 
"aggs": { 
    "agg_user": { 
     "terms": { 
     "field": "user" 
     } 
    }, 
    "agg_tag": { 
     "terms": { 
     "field": "tag" 
     } 
    } 
    } 
} 

您可以閱讀更多關於基於facet的d細溝向下例如這裏:http://distinctplace.com/2014/07/29/build-zappos-like-products-facets-with-elasticsearch/

(上述片段是奇蹟感腳本)