【问题标题】:Create an elasticsearch index with different query and index time analyzers使用不同的查询和索引时间分析器创建弹性搜索索引
【发布时间】:2016-02-03 20:27:50
【问题描述】:

虽然记录在案,但没有工作示例说明如何使用索引时间和不同的查询时间分析器创建索引。

我希望仅对搜索应用同义词过滤器。如果我指定分析器名称,我可以测试分析器,但没有名称,它不会检测到默认值。

可能出了什么问题?

"settings": {
      "index": {        
        "analysis": {
          "filter": {
            "synonym": {
              "type": "synonym",
              "synonyms": [
                "testword => otherword"
              ]
            }
          },
          "analyzer": {
            "default_search": {
              "filter": [
                "lowercase",
                "asciifolding",
                "synonym"
              ],
              "tokenizer": "standard"
            },
            "default_index": {
              "filter": [
                "lowercase",
                "asciifolding"
              ],
              "tokenizer": "standard"
            }
          }
        }

注意两个不同的分析器,名为default_searchdefault_index。根据文档,这些应该被选为默认值。因此,如果我执行对“testword”的搜索,它将改为搜索“otherword”。

我可以确认在索引类型上设置了默认分析器名称:

"myIndex": {
    "mappings": {
      "myType": {
        "index_analyzer": "default_index",
        "search_analyzer": "default_search",
        "properties": ...

我执行一个测试搜索:

在未指定分析器/myIndex/_analyze/?pretty=true&text=testword 的情况下调用(期望它按照配置拾取default_search

{
  "tokens" : [ {
    "token" : "testword",
    "start_offset" : 0,
    "end_offset" : 9,
    "type" : "<ALPHANUM>",
    "position" : 1
  } ]
}

使用特定分析器调用myIndex/_analyze/?analyzer=default_search&amp;pretty=true&amp;text=testword

{
  "tokens" : [ {
    "token" : "otherword",
    "start_offset" : 0,
    "end_offset" : 9,
    "type" : "SYNONYM",
    "position" : 1
  } ]
}

一个示例搜索,索引包含一个字段值为“otherword”的项目。下面的查询不返回任何结果,搜索“其他单词”会返回所需的项目。 POST myIndex/_search

"query": {
  "multi_match": {
    "query": "testword",
    "analyzer": "default_search",
    "fields": [
      "name"      
    ]
  }
}

【问题讨论】:

  • 请注意,用于索引的默认分析器应命名为default,而不是default_index(参见[此处])。(elastic.co/guide/en/elasticsearch/reference/2.2/…)。您还可以解释一下您是如何运行测试搜索的吗?
  • @Val 查看显示查询的更新问题
  • 你用的是什么版本的ES?
  • @ChintanShah25 我正在使用来自 bonsai.io 的托管版本 - 似乎是版本 1.7.2
  • 尝试通过 docker 使用版本 2.2.0 并遇到同样的问题。我确定我做错了什么!

标签: elasticsearch


【解决方案1】:

因为您调用的是_analyze 端点,所以这不是搜索。您实际上是在向 ES 发出请求并要求它分析您提供给它的令牌流好像 ES 正在索引这些令牌,所以默认的 indexing 分析器会启动。

如果您想尝试default_search 分析器,您需要向_search 端点发送请求。

您需要使用otherword 对文档进行索引,然后使用/_search?q=testword 进行搜索,您将看到default_search 分析器启动。

更新

您没有正确定义默认分析器,即您需要在 settings(而不是 mappings)中进行定义并正确命名它们(即 default 而不是 default_index)。

这是我用于测试的索引:

curl -XPUT localhost:9200/myindex -d '{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms": [
              "testword => otherword"
            ]
          }
        },
        "analyzer": {
          "default_search": {
            "filter": [
              "lowercase",
              "asciifolding",
              "synonym"
            ],
            "tokenizer": "standard"
          },
          "default": {
            "filter": [
              "lowercase",
              "asciifolding"
            ],
            "tokenizer": "standard"
          }
        }
      }
    }
  },
  "mappings": {
    "myType": {
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  }
}'

这是我为测试编制索引的示例文档:

curl -XPUT localhost:9200/myindex/myType/1 -d '{
  "name": "otherword"
}'

然后在不指定任何分析器的情况下使用以下查询,我可以找到上述文档:

curl -XPOST localhost:9200/myindex/myType/_search -d '{
  "query": {
    "multi_match": {
      "query": "testword",
      "fields": [
        "name"
      ]
    }
  }
}'

回复:

{
...
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "myindex",
      "_type" : "myType",
      "_id" : "1",
      "_score" : 0.30685282,
      "_source":{"name":"otherword"}
    } ]
  }
}

【讨论】:

  • 感谢@Val 的输入。我没有预见到_analyze 端点使用索引默认值而不是搜索。不幸的是,即使在查询中,搜索默认值也不会启动,即使我指定了它。我认为我必须将同义词逻辑移动到应用程序并在查询之前处理单词,因为 Elastic 似乎在提取它时遇到了问题。我已更新问题以显示示例查询。
  • 感谢您的帮助@Val。我从头开始再次尝试,它有效。在粘贴我的示例代码时,为了清楚起见,我已将实际值更改为示例值。我没有提到的是我的“其他词”实际上是“N°5”。我相信这个值被 asciifolding 过滤器删除了。因此,即使同义词有效,新值已经从索引中删除,因此不匹配结果。我正在尝试使用停用词。再次感谢。
  • 太棒了,很高兴你知道了!!
猜你喜欢
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 2021-02-03
  • 2016-09-04
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多