2017-02-07 227 views
0

嘗試在Elasticsearch中查詢w/PHP客戶端並優先部分詞匹配,但仍包含模糊匹配。如果我刪除了address.company匹配塊,查詢就會按預期工作,但無論我如何對其進行框架化處理,它都會隨之崩潰。我在格式化上迷失了,還包括了優先級較低的模糊搜索?Elasticsearch部分匹配或模糊匹配,提升部分結果

$search_data = [ 
     "from" => (int) $start, "size" => (int) $count, 
     'query' => [ 
      'bool' => [ 
       'filter' => [ 
        ['term' => ['active' => 1]], 
        ['term' => ['type' => 2]], 
       ], 
       'must' => [ 
        'wildcard' => [ 
         'address.company' => '*' . $search_query . '*' 
        ], 
        'match' => [ 
         'address.company' => [ 
          'query'  => $search_query, 
          'operator' => 'and', 
          'fuzziness' => 'AUTO', 
         ], 
        ], 
       ], 
      ], 
     ], 
    ]; 
+0

你能分享你的模式映射嗎 – user3775217

回答

0

儘管我對ES仍然很陌生,但這種解決方案似乎能夠獲得我所追求的數據。我只提到,因爲如果有人在將來看到這一點,可能會有一個更理想的方式。從必須切換到應該和包裝陣列有點不同的伎倆。

$search_data = [ 
    "from" => (int) $start, "size" => (int) $count, 
    'query' => [ 
     'bool' => [ 
      'filter' => [ 
       ['term' => ['active' => 1]], 
       ['term' => ['type' => 2]], 
      ], 
      'should' => [ 
       [ 
        'match' => ['address.company' => ['query'=>$search_query,'boost'=>10]], 
       ], 
       [ 
        'match' => 
        [ 
         'address.company' => 
          [ 
           'query'  => $search_query, 
           'fuzziness' => 'AUTO', 
          ], 
        ], 
       ], 
      ], 
      'minimum_should_match'=>1, 
     ], 
    ], 
];