目前我正在使用elasticsearch并尝试“搜索”群集中的文档。这是我没有得到预期结果的地方。我希望返回4个结果,因为它们都应该与查询关键字“te”匹配。 GET _search
{
"query": {
"filtered" : {
"filter" : {
"term" : {
"source_id" : 1
}
},
"query": {
"bool" : {
"must" : {
"term" : { "_all" : "te" }
}
}
}
}
},
"sort": [
{
"date": {
"order": "desc"
}
}
],
"from": 0,
"size": 5
}
当我运行此查询时,我只得到2个结果(而我期待4个)。当我删除“query:{}”部分时,我得到4个结果,其中包含以下“主题”字段:
{
"subject": ["Testbericht"]
"subject": ["test"]
"subject": ["Testbericht"]
"subject": ["Test to myself"]
}
查询中的过滤器仅返回特定源的结果(每个查询1个源)。
我的映射:
{
"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;
}
答案 0 :(得分:0)
您似乎在所有字符串字段中使用标准分析器。这个分析器使用小写字母,但它在空格和一些特殊字符上进行标记。您正在搜索" te",这只是部分匹配。它也不应该是测试和TestBericht的术语。我认为您提供的映射不正确,或者您有其他字段包含术语" te"就像在" te sterk"或者我忽略了一些东西。您是否还可以提供用于添加文档的命令以及完整的响应。