【问题标题】:ElasticSearch: strange search behaviour when using snowball analyzerElasticSearch:使用雪球分析器时的奇怪搜索行为
【发布时间】:2012-03-14 11:21:55
【问题描述】:

假设我有一个这样定义的 ElasticSearch 索引:

curl -XPUT 'http://localhost:9200/test' -d '{
  "mappings": {
    "example": {
      "properties": {
        "text": {
          "type": "string",
          "analyzer": "snowball"
        }
      }
    }
  }
}'

curl -XPUT 'http://localhost:9200/test/example/1' -d '{
  "text": "foo bar organization"
}'

当我使用雪球分析器搜索“foo organizations”时,两个关键字都符合预期:

curl -XGET http://localhost:9200/test/example/_search -d '{
  "query": {
    "text": {
      "_all": {
        "query": "foo organizations",
        "analyzer": "snowball"
      }
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}'

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.015912745,
    "hits": [
      {
        "_index": "test",
        "_type": "example",
        "_id": "1",
        "_score": 0.015912745,
        "_source": {
          "text": "foo bar organization"
        },
        "highlight": {
          "text": [
            "<em>foo</em> bar <em>organization</em>"
          ]
        }
      }
    ]
  }
}

但是当我只搜索“组织”时,我根本没有得到任何结果,这很奇怪:

curl -XGET http://localhost:9200/test/example/_search -d '{
  "query": {
    "text": {
      "_all": {
        "query": "organizations",
        "analyzer": "snowball"
      }
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}'

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

但是,如果我搜索“bars”,它仍然会命中:

curl -XGET http://localhost:9200/test/example/_search -d '{
  "query": {
    "text": {
      "_all": {
        "query": "bars",
        "analyzer": "snowball"
      }
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}'

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.10848885,
    "hits": [
      {
        "_index": "test",
        "_type": "example",
        "_id": "1",
        "_score": 0.10848885,
        "_source": {
          "text": "foo bar organization"
        },
        "highlight": {
          "text": [
            "foo <em>bar</em> organization"
          ]
        }
      }
    ]
  }
}

我猜“bar”和“organization”之间的区别在于“organization”的词根是“organ”,而“bar”的词根是它自己。但是如何获得正确的行为以便第二次搜索命中?

【问题讨论】:

    标签: elasticsearch snowball


    【解决方案1】:

    文本“foo bar organization”被索引两次 - 在字段 text 和字段 _all 中。 text 字段使用雪球分析器,_all 字段使用标准分析器。因此,在分析测试记录后,字段 _all 包含标记:“foo”、“bar”和“organization”。在搜索期间,指定的雪球分析器将“foo”转换为“foo”,将“bars”转换为“bar”,将“organization”转换为“organ”。因此,查询中的单词“foo”和“bars”与测试记录匹配,而术语“organization”则不匹配。突出显示是在每个字段的基础上执行的,独立于搜索,这就是为什么“组织”一词会在第一个结果中突出显示。

    【讨论】:

    • 这很有道理,谢谢。有没有办法可以查看我的索引具有的实际令牌?我可以告诉 ES 对 _all 列使用雪球吗?
    • 没有简单的方法可以查看索引中的实际标记,但可以查看如何使用分析 API (elasticsearch.org/guide/reference/api/…) 分析文本。您可以在创建索引gist.github.com/2043721 期间为 _all 字段设置雪球分析器。或者,可以将雪球设置为所有字段 gist.github.com/2043697 的默认分析器。
    【解决方案2】:

    在索引时使用分析器比在搜索时使用更好。将您的文本字段映射到雪球分析器然后索引。这将为包括组织在内的组织创建一些令牌。它适用于我

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2023-03-25
      • 2018-11-09
      相关资源
      最近更新 更多