Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ElasticSearch未返回正确的结果_Php_Search_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Php,Search,elasticsearch" /> elasticsearch,Php,Search,elasticsearch" />

Php ElasticSearch未返回正确的结果

Php ElasticSearch未返回正确的结果,php,search,elasticsearch,Php,Search,elasticsearch,目前我正在使用elasticsearch并尝试搜索集群中的文档。这就是我没有得到预期结果的地方。我希望得到4个返回的结果,因为它们都应该匹配查询关键字te。获取搜索 { "query": { "filtered" : { "filter" : { "term" : { "source_id" : 1 } }, "query": { "boo

目前我正在使用elasticsearch并尝试搜索集群中的文档。这就是我没有得到预期结果的地方。我希望得到4个返回的结果,因为它们都应该匹配查询关键字te。获取搜索

{
"query": {
   "filtered" : {
        "filter" : {
            "term" : {
                "source_id" : 1
            }
        },
        "query": {
            "bool" : {
                "must" : {
                    "term" : { "_all" : "te" }
                }
            }
        }
    }
}, 
"sort": [
  {
     "date": {
        "order": "desc"
     }
  }
], 
    "from": 0,
    "size": 5
}
当我运行这个查询时,我只得到2个结果,而我期望得到4个。当我删除查询:{}部分时,我得到4个结果,其中包含以下主题字段:

{
"subject": ["Testbericht"]
"subject": ["test"]
"subject": ["Testbericht"]
"subject": ["Test to myself"]
}
查询中的筛选器仅返回来自特定源的结果,每个查询返回一个源

我的映射:

{
   "messages": {
      "mappings": {
         "message": {
            "_id": {
               "index": "not_analyzed"
            },
            "properties": {
               "addresses": {
                  "type": "nested",
                  "properties": {
                     "displayname": {
                        "type": "string"
                     },
                     "email": {
                        "type": "string"
                     },
                     "name": {
                        "type": "string"
                     },
                     "type": {
                        "type": "string"
                     }
                  }
               },
               "body": {
                  "type": "string"
               },
               "date": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "files": {
                  "type": "nested",
                  "properties": {
                     "size": {
                        "type": "long"
                     },
                     "title": {
                        "type": "string"
                     },
                     "type": {
                        "type": "string"
                     }
                  }
               },
               "folders": {
                  "type": "nested",
                  "properties": {
                     "id": {
                        "type": "integer"
                     }
                  }
               },
               "size": {
                  "type": "long"
               },
               "source_id": {
                  "type": "integer"
               },
               "subject": {
                  "type": "string"
               }
            }
         }
      }
   }
}
当我尝试在_all=te上搜索时得到的结果

{
"subject": ["test"]
"subject": ["Testbericht"]
}
插入文件:

// PHP client from https://github.com/elasticsearch/elasticsearch-php
// $this->search = new Elasticsearch\Client();
// $id is an unique string
// $attributes is an array of the attributes
public function insert($id, array $attributes)
{
    $params = [
        'index' => self::INDEX,
        'type' => self::TYPE,
        'id' => $id,
        'body' => [
            'source_id' => $attributes['source_id'],
            'date' => $attributes['date']->format(DateTime::ISO8601),
            'size' => $attributes['size'],
            'subject' => $attributes['subject'],
            'body' => $attributes['body'],
            'addresses' => $attributes['addresses'],
            'files' => $attributes['files'],
            'folders' => $attributes['folders'],
        ],
    ];

    try
    {
        $this->search->index($params);

        return true;
    }
    catch(Exception $e)
    {
        throw new Exception($e->getMessage());
    }

    return false;
}

似乎在所有字符串字段中都使用了标准分析器。该分析器使用小写字母,但它对空格和一些特殊字符进行标记。您正在搜索te,它只是部分匹配。它也不应该是test和TestBericht的术语。我认为您提供的映射是不正确的,或者您有其他字段包含术语te,比如在对te sterk的描述中,或者我忽略了一些内容。您还可以提供用于添加文档的命令以及完整的响应。

运行查询时得到的两个结果是什么?还有,在索引时,分析器使用了什么?您已经发布了您的查询。请张贴您的地图以及。如果没有更多信息,我猜它当前映射为case sensetiveI,我没有使用任何特定的分析器,是吗?我使用的是他们的PHP客户端默认索引方法。我在帖子中添加了我的简单PHP函数。你知道我将如何匹配我想要的关键字吗?看看ngram或前缀ngram分析器,连同索引分析器和搜索分析器,你应该能够做到这一点。不幸的是,我没有真正做到这一点,不知何故,当我使用通配符通配符:{u all:te}选项时,我确实得到了正确的结果。这是一种正确的方法,还是资源非常昂贵?在执行查询时确实会使用更多的资源。使用ngram可以使用更多的磁盘空间和内存。ngrams通常具有更高的性能,尤其是如果使用前缀ngrams。