2016-09-29 4 views
-1

Ich versuche, ein Facettensystem mit Elasticsearch zu erstellen, um die Anzahl der Dokumente anzuzeigen, die einer Abfrage entsprechen.Elasticsearch Anwenden von Filtern auf Aggregation

ich zur Zeit mache diese Abfrage auf /_search?search_type=count:

{ 
    "query": { 
     "query_string": { 
      "query": "status:(1|2) AND categories:A" 
     } 
    }, 
    "aggs": { 
     "all_products": { 
      "global": {}, 
      "aggs": { 
       "countries": { 
        "aggs": { 
         "counter": { 
          "terms": ["min_doc_count": 0, "field": "country"], 
          "aggs": ["unique": ["cardinality": ["field": "id"]]] 
         } 
        } 
       }, 
       "categories": { 
        "aggs": { 
         "counter": { 
          "terms": ["min_doc_count": 0, "field": "category"], 
          "aggs": ["unique": ["cardinality": ["field": "id"]]] 
         } 
        } 
       }, 
       "statuses": { 
        "aggs": { 
         "counter": { 
          "terms": ["min_doc_count": 0, "field": "status"], 
          "aggs": ["unique": ["cardinality": ["field": "id"]]] 
         } 
        } 
       } 
      } 
     } 
    } 
} 

die Dokumente haben die folgende Struktur:

{ 
    "id": 123, 
    "name": "Title", 
    "categories": ["A", "B", "C"], 
    "country": "United Kingdom", 
    "status": 1 
} 

so die Ausgabe, die ich suche sollte sein:

Land

  • UK: 123
  • USA: 1000

Kategorie

  • Motors: 23
  • Fashion: 1100

Der Status

  • aktiv: 1120
  • Nicht aktiv: 3

Ich weiß nicht, wie man richtig die Aggregationen filtern, weil gerade sie in dem angegebenen Feld alle das Dokument zählen, ohne die Berücksichtigung Abfrage status:(1|2) AND categories:A.

Die elastische Version ist 1.7.2.

Antwort

0

Sie müssen lediglich global Aggregation zu entfernen, da sie von der Abfrage nicht beeinflusst wird, bewegen Sie einfach Ihre countries, categories und statuses Aggregationen auf der obersten Ebene wie folgt aus:

{ 
    "query": { 
     "query_string": { 
      "query": "status:(1|2) AND categories:A" 
     } 
    }, 
    "aggs": { 
      "countries": { 
       "aggs": { 
        "counter": { 
         "terms": ["min_doc_count": 0, "field": "country"], 
         "aggs": ["unique": ["cardinality": ["field": "id"]]] 
        } 
       } 
      }, 
      "categories": { 
       "aggs": { 
        "counter": { 
         "terms": ["min_doc_count": 0, "field": "category"], 
         "aggs": ["unique": ["cardinality": ["field": "id"]]] 
        } 
       } 
      }, 
      "statuses": { 
       "aggs": { 
        "counter": { 
         "terms": ["min_doc_count": 0, "field": "status"], 
         "aggs": ["unique": ["cardinality": ["field": "id"]]] 
        } 
       } 
      } 
     } 

} 
+0

etwas Glück dabei? – Val

0

Fabio. Ich sehe Deinen Beitrag zum upwork, ich habe zB für ES 2.4 gearbeitet, vielleicht hilft es Dir.

"index": "{{YOUR ELASTIC INDEX}}", 
    "type": "{{YOUR ELASTIC TYPE}}", 
    "body": { 
     "aggs": { 
      "trademarks": { // aggs NAME 
       "terms": { 
        "field": "id", // field name in ELASTIC base 
        "size": 100 // count of results YOU need 
       } 
      }, 
      "materials": { //another aggs NAME 
       "terms": { 
        "field": "materials.name", // field name in ELASTIC base 
        "size": 100/count of results YOU need 
       } 
      }, 
      "certificate": { 
       "terms": { 
        "field": "certificate_type_id", 
        "size": 100 
       } 
      }, 
      "country": { 
       "terms": { 
        "field": "country.id", 
        "size": 100 
       } 
      }, 
      "price": { 
       "stats": { 
        "field": "price" 
       } 
      } 
     }, 
     "from": 0, // start from 
     "size": 20, // results count 
     "query": { 
      "constant_score": { 
       "filter": { //apply filter 
        "bool": { 
         "should": [{ // all categories You need to show 
          "term": { 
           "categories": "10142" 
          } 
         }, { 
          "term": { 
           "categories": "10143" 
          } 
         }, { 
          "term": { 
           "categories": "10144" 
          } 
         }, { 
          "term": { 
           "categories": "10145" 
          } 
         }, { 
          "term": { 
           "categories": "12957" 
          } 
         }, { 
          "term": { 
           "categories": "13968" 
          } 
         }, { 
          "term": { 
           "categories": "14353" 
          } 
         }, { 
          "term": { 
           "categories": "16954" 
          } 
         }, { 
          "term": { 
           "categories": "18243" 
          } 
         }, { 
          "term": { 
           "categories": "10141" 
          } 
         }], 
         "must": [{ // if you want another filed to filter for example filter BY field trademark_id 
          "bool": { 
           "should": [{ 
            "term": { 
             "trademark_id": "2872" 
            } 
           }, { 
            "term": { 
             "trademark_id": "2879" 
            } 
           }, { 
            "term": { 
             "trademark_id": "2914" 
            } 
           }] 
          } 
         }, { 
          "bool": { // filter by PRICE 
           "must": [{ 
            "range": { 
             "price": { 
              "from": 5.97, 
              "to": 15752.69 
             } 
            } 
           }] 
          } 
         }] 
        } 
       } 
      } 
     }, 
     "sort": { //here SORT BY desc or asc 
      "updated_at": "desc" //updated_at - field from ES base 
     } 
    } 
+0

Ich benutze es für die Sammlung von Waren in meinem Internetshop – Valery

Verwandte Themen