2016-06-22 14 views
0

erste Array:ich habe zwei Arrays, hwo, um diese beiden Arrays zu kombinieren bereiten Sie das JSON-Array?

Array 
(
    [must] => Array 
     (
      [bool] => Array 
       (
        [should] => Array 
         (
          [0] => Array 
           (
            [term] => Array 
             (
              [filter_system_memory] => 2 GB - 4 GB 
             ) 

           ) 

          [1] => Array 
           (
            [term] => Array 
             (
              [filter_system_memory] => 1 GB -2 GB 
             ) 

           ) 

         ) 

       ) 

     ) 

) 

secound Array:

Array 
(
    [must] => Array 
     (
      [bool] => Array 
       (
        [should] => Array 
         (
          [0] => Array 
           (
            [term] => Array 
             (
              [filter_brand] => Dell 
             ) 

           ) 

          [1] => Array 
           (
            [term] => Array 
             (
              [filter_brand] => Sony 
             ) 

           ) 

          [2] => Array 
           (
            [term] => Array 
             (
              [filter_brand] => Lenovo 
             ) 

           ) 

         ) 

       ) 

     ) 

) 

Ich kann über zwei Arrays fusionieren, da beide Arrays gleichen Schlüssel haben, so Schlüssel zu überschreiben. Ich bekomme kein genaues JSON-Objekt wie unten. Dieses Problem wurde in der Elasti-Suche behandelt. pls mich leiten wie kombiniert diese beiden Arrays json Objekt wie unter

{ 
     "must": { 
     "bool":{ 
     "should": [ 
     { "term": { "filter_brand": "Dell" }}, 
     { "term": { "filter_brand": "Sony" }}, 
     { "term": { "filter_brand": "Lenovo" }} 
     ] 
     } 
     }, 
     "must": { 
      "bool":{ 
      "should": [ 
      { "term": { "filter_system_memory": "2 GB - 4 GB" }}, 
      { "term": { "filter_system_memory": "1 GB -2 GB" }} 
     ] 
     } 
     } 
    } 

Antwort

0

bereite ich diese Abfrage verwenden eine AND-Abfrage zu tun

"filter_brand" == dell or sony or lenovo 
AND 
"filter_system_memory" == "2 GB - 4 GB" or "1 GB -2 GB" 

die Abfrage:

{ 
    "query" : { 
     "filtered" : { 
      "filter" : { 
       "and" : [{ 
         "or" : [{ 
           "term" : { 
            "filter_brand" : "Dell", 
            "_cache" : false 
           } 
          }, { 
           "term" : { 
            "filter_brand" : "Sony", 
            "_cache" : false 
           } 
          }, { 
           "term" : { 
            "filter_brand" : "Lenovo", 
            "_cache" : false 
           } 
          } 
         ] 
        }, { 
         "or" : [{ 
           "term" : { 
            "filter_system_memory" : "2 GB - 4 GB", 
            "_cache" : false 
           } 
          }, { 
           "term" : { 
            "filter_system_memory" : "1 GB -2 GB" 
            "_cache" : false 
           } 
          } 
         ] 
        } 
       ] 
      } 
     } 
    } 
} 
+0

ok das ist in Ordnung dank –

+0

i wo Indexsuche in über json setzen kann auch den Suchindex übergeben müssen? –

+0

Ich habe es danken Ihnen sehr es ist nützlich für mich. –

0

Ihr gewünschtes Objekt ist nicht gültig, da must als Schlüssel dupliziert wird.

Sie könnten ein Array erstellen, die wie folgt aussieht:

[ 
    {"must": { 
    "bool":{ 
     "should": [ 
     { "term": { "filter_brand": "Dell" }}, 
     { "term": { "filter_brand": "Sony" }}, 
     { "term": { "filter_brand": "Lenovo" }} 
     ] 
    } 
    } 
    }, 
    {"must": { 
    "bool":{ 
    "should": [ 
     { "term": { "filter_system_memory": "2 GB - 4 GB" }}, 
     { "term": { "filter_system_memory": "1 GB -2 GB" }} 
    ] 
    } 
    } 
    } 
] 

$combinedArray = [$firstArray, $secondArray];

+0

elasticsearch funktionieren nicht so. Sie können ihm keine Abfrage senden – AlainIb

Verwandte Themen