2012-01-18 2 views
10

Solr에서 ES로 변경하려고합니다. 정보를 찾을 수없는 것 중 하나는 패싯을 만들 때 ES에서 제외 필터를 정의 할 수 있는지 여부입니다.탄성 검색 : 패싯 가능한 동안 필터 제외? (Solr에서와 같이)

예를 들어 producttype의 값을 A,B,C (예 : 표시 횟수)로 설정합니다. 또한 쿼리가 producttype: A으로 제한되어 있다고 생각하십시오.

이 경우 Solr은 producttype: A의 영향을 피하기 위해 producttype에 영향을 미치지 않도록 지정합니다. IOW, producttype의 수는 producttype: A 제약이 적용되지 않은 것처럼 표시됩니다.

볼 방법 SOLR에서이 작업을 수행하려면 : http://wiki.apache.org/solr/SimpleFacetParameters> 태그 및 제외 필터

ElasticSearch에서이 작업을 수행 할 수있는 방법이 있나요

?

답변

13

가능합니다.

쿼리 DSL에서 필터를 사용할 수 있지만 검색 API는 패싯 계산 후 검색 결과를 필터링하는 데 사용되는 최상위 filter 매개 변수도 허용합니다. 예를 들어

:

curl -XPUT 'http://127.0.0.1:9200/my_index/?pretty=1' -d ' 
{ 
    "mappings" : { 
     "product" : { 
     "properties" : { 
      "product_type" : { 
       "index" : "not_analyzed", 
       "type" : "string" 
      }, 
      "product_name" : { 
       "type" : "string" 
      } 
     } 
     } 
    } 
} 
' 

2) 색인 일부 문서 (:

1) 첫째, 색인을 생성하고, 당신이 product_type를 원하기 때문에 열거로 취급 할 수는 not_analyzed로 설정 노트, 문서 3) 다른 product_name 있습니다

curl -XPUT 'http://127.0.0.1:9200/my_index/product/1?pretty=1' -d ' 
{ 
    "product_type" : "A", 
    "product_name" : "foo bar" 
} 
' 
curl -XPUT 'http://127.0.0.1:9200/my_index/product/2?pretty=1' -d ' 
{ 
    "product_type" : "B", 
    "product_name" : "foo bar" 
} 
' 
curl -XPUT 'http://127.0.0.1:9200/my_index/product/3?pretty=1' -d ' 
{ 
    "product_type" : "C", 
    "product_name" : "bar" 
} 
' 

3) 이름 012,380,815,457을 포함 제품에 대한 검색을 수행

curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d ' 
{ 
    "query" : { 
     "text" : { 
     "product_name" : "foo" 
     } 
    }, 
    "filter" : { 
     "term" : { 
     "product_type" : "A" 
     } 
    }, 
    "facets" : { 
     "product_type" : { 
     "terms" : { 
      "field" : "product_type" 
     } 
     } 
    } 
} 
' 

# { 
# "hits" : { 
#  "hits" : [ 
#   { 
#    "_source" : { 
#    "product_type" : "A", 
#    "product_name" : "foo bar" 
#    }, 
#    "_score" : 0.19178301, 
#    "_index" : "my_index", 
#    "_id" : "1", 
#    "_type" : "product" 
#   } 
#  ], 
#  "max_score" : 0.19178301, 
#  "total" : 1 
# }, 
# "timed_out" : false, 
# "_shards" : { 
#  "failed" : 0, 
#  "successful" : 5, 
#  "total" : 5 
# }, 
# "facets" : { 
#  "product_type" : { 
#   "other" : 0, 
#   "terms" : [ 
#    { 
#    "count" : 1, 
#    "term" : "B" 
#    }, 
#    { 
#    "count" : 1, 
#    "term" : "A" 
#    } 
#   ], 
#   "missing" : 0, 
#   "_type" : "terms", 
#   "total" : 2 
#  } 
# }, 
# "took" : 3 
# } 

4)에 대한 검색을 수행하여 (의사 3 때문에 product_typeC 제외) 29,783,369,543,210 후 product_type == A하여 검색 결과를 필터링하여 상기 product_namefoo있는 모든 문서에 대한 product_type 대한 패싯을 계산할 인덱스에서의 global 매개 변수를 지정하여 모든 제품에 대한 product_name에서 foo하지만, 계산 측면 :

# [Wed Jan 18 17:15:09 2012] Protocol: http, Server: 192.168.5.10:9200 
curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d ' 
{ 
    "query" : { 
     "text" : { 
     "product_name" : "foo" 
     } 
    }, 
    "filter" : { 
     "term" : { 
     "product_type" : "A" 
     } 
    }, 
    "facets" : { 
     "product_type" : { 
     "global" : 1, 
     "terms" : { 
      "field" : "product_type" 
     } 
     } 
    } 
} 
' 

# [Wed Jan 18 17:15:09 2012] Response: 
# { 
# "hits" : { 
#  "hits" : [ 
#   { 
#    "_source" : { 
#    "product_type" : "A", 
#    "product_name" : "foo bar" 
#    }, 
#    "_score" : 0.19178301, 
#    "_index" : "my_index", 
#    "_id" : "1", 
#    "_type" : "product" 
#   } 
#  ], 
#  "max_score" : 0.19178301, 
#  "total" : 1 
# }, 
# "timed_out" : false, 
# "_shards" : { 
#  "failed" : 0, 
#  "successful" : 5, 
#  "total" : 5 
# }, 
# "facets" : { 
#  "product_type" : { 
#   "other" : 0, 
#   "terms" : [ 
#    { 
#    "count" : 1, 
#    "term" : "C" 
#    }, 
#    { 
#    "count" : 1, 
#    "term" : "B" 
#    }, 
#    { 
#    "count" : 1, 
#    "term" : "A" 
#    } 
#   ], 
#   "missing" : 0, 
#   "_type" : "terms", 
#   "total" : 3 
#  } 
# }, 
# "took" : 4 
# } 

OP 확장 질문에 대한 답변 :

facet_filters이라고하는 각 패싯에 직접 필터를 적용 할 수도 있습니다.

이전에

유사 예 :

1) 인덱스를 만듭니다

curl -XPUT 'http://127.0.0.1:9200/my_index/?pretty=1' -d ' 
{ 
    "mappings" : { 
     "product" : { 
     "properties" : { 
      "color" : { 
       "index" : "not_analyzed", 
       "type" : "string" 
      }, 
      "name" : { 
       "type" : "string" 
      }, 
      "type" : { 
       "index" : "not_analyzed", 
       "type" : "string" 
      } 
     } 
     } 
    } 
} 
' 

2) 인덱스 일부 데이터 :

curl -XPUT 'http://127.0.0.1:9200/my_index/product/1?pretty=1' -d ' 
{ 
    "color" : "red", 
    "name" : "foo bar", 
    "type" : "A" 
} 
' 

curl -XPUT 'http://127.0.0.1:9200/my_index/product/2?pretty=1' -d ' 
{ 
    "color" : [ 
     "red", 
     "blue" 
    ], 
    "name" : "foo bar", 
    "type" : "B" 
} 
' 

curl -XPUT 'http://127.0.0.1:9200/my_index/product/3?pretty=1' -d ' 
{ 
    "color" : [ 
     "green", 
     "blue" 
    ], 
    "name" : "bar", 
    "type" : "C" 
} 
' 

3) 검색, 모두 type이 제품에 대한 필터링 == Acolor == blue 인 경우 "기타"필터를 제외한 각 속성에서 패싯을 실행하십시오.

curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d ' 
{ 
    "filter" : { 
     "and" : [ 
     { 
      "term" : { 
       "color" : "blue" 
      } 
     }, 
     { 
      "term" : { 
       "type" : "A" 
      } 
     } 
     ] 
    }, 
    "facets" : { 
     "color" : { 
     "terms" : { 
      "field" : "color" 
     }, 
     "facet_filter" : { 
      "term" : { 
       "type" : "A" 
      } 
     } 
     }, 
     "type" : { 
     "terms" : { 
      "field" : "type" 
     }, 
     "facet_filter" : { 
      "term" : { 
       "color" : "blue" 
      } 
     } 
     } 
    } 
} 
' 

# [Wed Jan 18 19:58:25 2012] Response: 
# { 
# "hits" : { 
#  "hits" : [], 
#  "max_score" : null, 
#  "total" : 0 
# }, 
# "timed_out" : false, 
# "_shards" : { 
#  "failed" : 0, 
#  "successful" : 5, 
#  "total" : 5 
# }, 
# "facets" : { 
#  "color" : { 
#   "other" : 0, 
#   "terms" : [ 
#    { 
#    "count" : 1, 
#    "term" : "red" 
#    } 
#   ], 
#   "missing" : 0, 
#   "_type" : "terms", 
#   "total" : 1 
#  }, 
#  "type" : { 
#   "other" : 0, 
#   "terms" : [ 
#    { 
#    "count" : 1, 
#    "term" : "C" 
#    }, 
#    { 
#    "count" : 1, 
#    "term" : "B" 
#    } 
#   ], 
#   "missing" : 0, 
#   "_type" : "terms", 
#   "total" : 2 
#  } 
# }, 
# "took" : 3 
# } 
+0

감사합니다. 이 간단한 경우에는 작동합니다. 두 가지 속성 (예 :'productcategory' 및'color')이 있으면 어떨까요? 두 속성 모두에 대해 파셋을 적용하고 필터링중인 속성에 설정된 필터를 제외하고 싶습니다. 그래서'productcategory'에 면체 처리를하면'productcategory' 필터가 제외되고 'color'에 면체 처리가 적용되면'color' 필터가 제외됩니다. 이 경우 나는 global을 만드는 것이 효과가 없다. (나는 믿는다.)'productcategory'를 사용하여 가능한'color' 필터를 고려해야하고 그 반대도 마찬가지입니다. 나는 이것이 다른 질문 btw임을 깨닫는다. –

+0

나는 그것을 수행하는 방법을 보여주기 위해 위의 대답을 확장했습니다. – DrTech

+0

facet_filters의 존재를 알아두면 좋습니다. 패싯에서 'facet_filters'를 지정하면 (전역) 필터 세트가 무시된다는 것을 정확히 이해합니까? 나는 전역 적으로 설정된 필터를 무시하는면을 지시하기 위해면에'global : 1'을 설정해야한다고 생각했을 것입니다. –