首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElasticSearch:使用snowball分析器时出现奇怪的搜索行为

ElasticSearch:使用snowball分析器时出现奇怪的搜索行为
EN

Stack Overflow用户
提问于 2012-03-14 19:21:55
回答 2查看 2.1K关注 0票数 1

假设我有一个定义如下的ElasticSearch索引:

代码语言:javascript
复制
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"
}'

当我用snowball analyzer搜索"foo organizations“时,两个关键字都像预期的那样匹配:

代码语言:javascript
复制
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>"
          ]
        }
      }
    ]
  }
}

但当我只搜索“组织”时,我根本得不到任何结果,这非常奇怪:

代码语言:javascript
复制
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": []
  }
}

但是,如果我搜索“bar”,结果仍然是:

代码语言:javascript
复制
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”的区别在于,“组织”源于“器官”,而"bar“源于自身。但是,我如何获得正确的行为,以便第二次搜索命中?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-14 21:18:19

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

票数 1
EN

Stack Overflow用户

发布于 2014-03-06 23:52:49

最好在索引时使用分析器,而不是先将文本字段搜索到雪球分析器,然后再搜索time..Map。这将为组织创建一些令牌,其中包括organizations.It works for me

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9700962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档