elasticsearch - 如何使用 Elasticsearch 在查询时指定不同的分析器?

标签 elasticsearch

我想在查询时使用不同的分析器来编写我的查询。

我从文档“Controlling Analysis”中了解到这是可能的:

[...] the full sequence at search time:

  • The analyzer defined in the query itself, else
  • The search_analyzer defined in the field mapping, else
  • The analyzer defined in the field mapping, else
  • The analyzer named default_search in the index settings, which defaults to
  • The analyzer named default in the index settings, which defaults to
  • The standard analyzer

但我不知道如何编写查询以便为不同的子句指定不同的分析器:

"query"  => [
    "bool" => [
        "must"   => [
            {
                "match": ["my_field": "My query"]
                "<ANALYZER>": <ANALYZER_1>
            }
        ],
        "should" => [
            {
                "match": ["my_field": "My query"]
                "<ANALYZER>": <ANALYZER_2>    
            }
        ]
    ]
]

我知道我可以索引两个或多个不同的字段,但我有很强的二级内存限制,我无法索引相同的信息 N 次。

谢谢

最佳答案

如果您还没有,您首先需要将自定义分析器映射到您的索引设置端点。

注意:如果索引存在并且正在运行,请务必先将其关闭。

POST/my_index/_close

然后将自定义分析器映射到设置端点。

PUT /my_index/_settings
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_analyzer1": { 
          "type": "standard",
          "stopwords_path": "stopwords/stopwords.txt"
        },
        "custom_analyzer2": { 
          "type": "standard",
          "stopwords": ["stop", "words"]
        }
      }
    }
  }
}

再次打开索引。

POST/my_index/_open

现在您可以使用新的分析器查询您的索引。

GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [{
        "match": {
          "field_1": {
            "query": "Hello world",
            "analyzer": "custom_analyzer1"
          }
        }
      }],
      "must": [{
        "match": {
          "field_2": {
            "query": "Stop words can be tough",
            "analyzer": "custom_analyzer2"
          }
        }
      }]
    }
  }
}

关于elasticsearch - 如何使用 Elasticsearch 在查询时指定不同的分析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37754132/

相关文章:

elasticsearch - 如何忽略river mongodb和elasticsearch中的一些字段?

c# - NEST返回null而不是字段

java - Elasticsearch 5 Java 客户端给出 "NoNodeAvailableException"

c# - NEST 2.0和ElasticSearch 2.0无法模拟 “return all”查询

elasticsearch - 如何在Kibana 4中设计多X轴图

elasticsearch - 如何配置 elasticsearch 将文档保留最多 30 天?

Elasticsearch 还在初始化 kibana 索引

python - 对连接的字符串进行标记

Elasticsearch如何返回每个文档的匹配词条数

geolocation - ElasticSearch 脚本字段返回不正确的经度值