【问题标题】:ElasticSearch not returning proper resultsElasticSearch 没有返回正确的结果
【发布时间】:2014-09-13 11:43:40
【问题描述】:

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

{
"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"]
}

查询中的过滤器仅返回来自特定来源的结果(每个查询 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;
}

【问题讨论】:

  • 您在运行查询时得到的两个结果是什么?还有索引时使用的分析器是什么?
  • 您已发布查询。请同时发布您的映射。如果没有更多信息,我猜它目前被映射为区分大小写
  • 我没有使用任何特定的分析仪,是吗?

标签: php search elasticsearch


【解决方案1】:

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

【讨论】:

  • 我正在使用他们的 PHP 客户端(默认索引方法)。我已将我的(简单)PHP 函数添加到我的帖子中。知道如何匹配我想要的关键字吗?
  • 查看 ngram 或 prefix-ngram 分析器,连同索引分析器和搜索分析器,您应该能够做到这一点。
  • 不幸的是,我并没有真正到达那里,不知何故,当我使用通配符 ("wildcard":{"_all":"te"}) 选项时,我确实得到了正确的结果。这是一种正确的方法还是非常耗费资源?
  • 它在执行查询时确实使用了更多资源。使用 ngrams 可以使用更多的磁盘空间和内存。 ngrams 通常会有更高的性能,特别是如果你使用前缀 ngrams。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2012-08-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
相关资源
最近更新 更多