弹性搜索match_pharse查询无法正常工作

时间:2018-06-11 02:08:43

标签: java elasticsearch kibana elastic-stack

我尝试使用kibana中的<?php /******************************************************* * Only these origins will be allowed to upload images * ******************************************************/ $accepted_origins = array("http://localhost:8000", "http://192.168.1.1", "http://example.com"); /********************************************* * Change this line to set the upload folder * *********************************************/ $today = date("y-m-d"); $imageFolder = "articles/assets/images/"; reset ($_FILES); $temp = current($_FILES); if (is_uploaded_file($temp['tmp_name'])){ if (isset($_SERVER['HTTP_ORIGIN'])) { // same-origin requests won't set an origin. If the origin is set, it must be valid. if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) { header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); } else { header("HTTP/1.1 403 Origin Denied"); return; } } /* If your script needs to receive cookies, set images_upload_credentials : true in the configuration and enable the following two headers. */ // header('Access-Control-Allow-Credentials: true'); // header('P3P: CP="There is no P3P policy."'); // Sanitize input if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) { header("HTTP/1.1 400 Invalid file name."); return; } // Verify extension if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) { header("HTTP/1.1 400 Invalid extension."); return; } // Accept upload if there was no origin, or if it is an accepted origin $filetowrite = $imageFolder . $temp['name']; move_uploaded_file($temp['tmp_name'], $filetowrite); // Respond to the successful upload with JSON. // Use a location key to specify the path to the saved image resource. // { location : '/your/uploaded/image/file'} echo json_encode(array('location' => $filetowrite)); } else { // Notify editor that the upload failed header("HTTP/1.1 500 Server Error"); } ?> 查询搜索以下文档,但未收到回复。

请在下面找到有弹性搜索的文件

match_phrase

请找到用于搜索上述文档的查询。

    {  
       "took":7,
       "timed_out":false,
       "_shards":{  
          "total":5,
          "successful":5,
          "skipped":0,
          "failed":0
       },
       "hits":{  
          "total":2910,
          "max_score":1.0,
          "hits":[  
             {  
                "_index":"documents",
                "_type":"doc",
                "_id":"DmLD22MBFTg0XFZppYt8",
                "_score":1.0,
                "_source":{  
                   "doct_country":"DE",
                   "filename":"series_Accessories_v1_de-DE.pdf",


             }

          ]
       }
    }

对于上述查询,我​​得到了这个回复:

GET documents/_search
{
    "query": {
        "match_phrase" : {
            "message" : "Accessories_v1_de-DE.pdf"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

有两个问题。大概在您的查询中,您的意思是使用filename字段而不是message,而示例文档中不存在该字段:

GET documents/_search
{
    "query": {
        "match_phrase" : {
            "filename" : "Accessories_v1_de-DE.pdf"
        }
    }
}

其次,您需要Elasticsearch知道应将filename字段编入索引,并将_视为拆分。默认情况下不会发生这种情况。一种方法是按如下方式定义映射:

PUT /documents
{
    "mappings" : {
        "document" : {
            "properties" : {
                "filename" : { "type" : "text", "analyzer": "simple" }
            }
        }
    }
}

简单分析器将拆分为任何非字母,因此_和数字将被视为拆分。根据您的应用程序,您可能需要对标记化进行更精细的控制。请参阅documentation