elasticsearch,Php,elasticsearch" /> elasticsearch,Php,elasticsearch" />

Php Elasticsearch-如何在查询中使用多个分析器

Php Elasticsearch-如何在查询中使用多个分析器,php,elasticsearch,Php,elasticsearch,我想在查询中实现同义词和停止词过滤器。为此,我创建了两个分析器,它们各自都工作得很好。但我想两者兼用,我怎么能 GET my_index/_search/ { "query": { "match": { "_all": { "query": "Good and Bad", "analyzer": [ "stop_analyzer",

我想在查询中实现同义词停止词过滤器。为此,我创建了两个分析器,它们各自都工作得很好。但我想两者兼用,我怎么能

GET my_index/_search/
{
    "query": {
        "match": {
           "_all": {
             "query": "Good and Bad",
             "analyzer": [
                 "stop_analyzer",
                 "synonym"
             ]
           }
        }
    }
}
上面的查询引发了一个错误:

{
   "error": {
      "root_cause": [
         {
            "type": "parsing_exception",
            "reason": "[match] unknown token [START_ARRAY] after [analyzer]",
            "line": 6,
            "col": 26
         }
      ],
      "type": "parsing_exception",
      "reason": "[match] unknown token [START_ARRAY] after [analyzer]",
      "line": 6,
      "col": 26
   },
   "status": 400
}
我想我不能像使用单个分析器那样使用数组或对象,比如
“分析器”:“停止分析器”
“分析器”:“同义词”
,它工作得很好。所以我的问题是如何使用这两种分析器?

您可以定义一个,它可以将这两个简单的分析器组合成一个复合体

定义自定义分析器 假设您已按以下方式创建索引:

PUT my_index
{  
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "stopwordsSynonym": {
            "filter": [
              "lowercase",
              "my_synonym",
              "english_stop"
            ],
            "tokenizer": "standard"
          }
        },
        "filter": {
          "english_stop": {
            "type": "stop",
            "stopwords": "_english_"
          },
          "my_synonym": {
            "type": "synonym",
            "synonyms": [
              "nice => good",
              "poor => bad"  
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "my_type": {
        "properties": {
            "my_text": {
                "type": "text",
                "analyzer": "stopwordsSynonym"
            }
        }
    }
  }
}
并增加了一项记录:

POST my_index/my_type
{
    "my_text": "People aren’t born good or bad. Maybe they’re born with tendencies either way, but it’s the way you live your life that matters."
}
现在,默认情况下,对
my_text
的搜索将使用
stopwordsSynonym
分析器。此查询将与文档匹配,因为
nice
good
的同义词:

GET my_index/_search
{
    "query": {
        "match": {
            "my_text": "nice and ugly"
        }
    }
}
测试自定义分析器 您也可以这样测试分析仪:

GET my_index/_analyze 
{
  "analyzer": "stopwordsSynonym", 
  "text":     "nice or ugly"
}

{
   "tokens": [
      {
         "token": "good",
         "start_offset": 0,
         "end_offset": 4,
         "type": "SYNONYM",
         "position": 0
      },
      {
         "token": "ugly",
         "start_offset": 8,
         "end_offset": 12,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}
事实上,
stopwordsSynonym
nice
代币替换为
good
(其
类型
同义词
),并从代币列表中删除
,因为它是一个常见的英语stopWords

为查询定义分析器 为了对给定查询使用不同的分析器,可以使用查询:

或查询:

在任何情况下,应在创建时将
analyzer
添加到索引的设置中(请参见答案的开头)


再看一看,它允许使用不同的分析器进行搜索。

你能给我举个例子吗?@userpk当然,更新了答案。告诉我是否有用!谢谢你的详细回答。
GET my_index/_analyze 
{
  "analyzer": "standard", 
  "text":     "nice or ugly"
}

{
   "tokens": [
      {
         "token": "nice",
         "start_offset": 0,
         "end_offset": 4,
         "type": "<ALPHANUM>",
         "position": 0
      },
      {
         "token": "or",
         "start_offset": 5,
         "end_offset": 7,
         "type": "<ALPHANUM>",
         "position": 1
      },
      {
         "token": "ugly",
         "start_offset": 8,
         "end_offset": 12,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}
GET /_search
{
    "query": {
        "query_string": {
            "query": "my_text:nice and poor",
            "analyzer": "stopwordsSynonym"
        }
    }
}
GET my_index/_search
{
    "query": {
        "match_phrase" : {
            "my_standard_text" : {
                "query" : "nice and poor",
                "analyzer": "stopwordsSynonym"
            }
        }
    }
}