在ElasticSearch 2.X中,当我想查询Java API中特定类型的特定字段时,我可以这样做:
public List<String> getNames(String index, String type) {
List<String> names = new ArrayList<>();
SearchResponse scrollResp = client.prepareSearch()
.setIndices(index)
.setTypes(type)
.addFields("my_name")
.setQuery(QueryBuilders.matchAllQuery())
.setSize(10).execute().actionGet();
for (SearchHit hit : scrollResp.getHits().getHits()) {
Map<String, SearchHitField> fields = hit.getFields();
String name = fields.get("my_name").value();
names.add(name);
}
return names;
}
然而,在ES 5.0中,&#34; addFields&#34;方法消失了。有一些名为&#34; addStoredField&#34;。但是,如果您没有在映射中明确标记存储的字段,则这似乎不起作用。相反&#34; hit.getFields()&#34;对于上面的代码,只返回一组空字段(fields.size()== 0)。
如何为5.X获得与2.X相同的功能?也就是说,从结果中获取特定字段,而不显式存储它们。?
我在这里使用PreBuiltTransportClient作为客户端。
答案 0 :(得分:2)
如果您使用if($_POST['submit']== 'Upload_picture')
{
//echo "<pre>"; print_r($_FILES['upload_picture']['name']);
//die;
$album_title = $_GET['name'];
$album_id = $_GET['album'];
$album_dir = "../images/album/$album_title/"; #album path root directory
$db_album_dir = "images/album/$album_title/"; #batadase album path root directory
$error = array();
$extension = array('jpg','gif','png','jpeg');
foreach($_FILES['upload_picture']['tmp_name'] as $key => $tmp_name){
$file_name = $_FILES['upload_picture']['name'][$key];
$file_tmp = $_FILES['upload_picture']['tmp_name'][$key];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_filename = rand().".".$ext; #changing name
if(in_array($ext,$extension))
{
if(move_uploaded_file($file_tmp= $_FILES['upload_picture']['tmp_name'][$key],"$album_dir$new_filename"))
{
# insert record database
$values = [
'album_id' =>$album_id,
'image_name' => $new_filename,
'album_name' => $album_title,
'image_path' => $db_album_dir.$new_filename,
'uploaded_date' => date("Y/m/d h:i:s a")
];
include_once "action_page.php";
$tablename = 'album_picture';
$abc = new Demo();
$res = $abc->insert($tablename,$values);
unset($abc);
$_SESSION['upload_success'] = "Files Uploaded succesfully";
header("location:../upload_album.php?album=$album_id&name=$album_title");
}
else
{
$_SESSION['upload_error'] = "Something went wrong, files cannot be uploaded";
}
}
else
{
$_SESSION['upload_warning'] = "Please upload file";
header("location:../upload_album.php?album=$album_id&name=$album_title");
}
}//EOF FROEACH
方法定义您正在使用的字段,该怎么办:
.setFetchSource
您也可以查看此SO。