Elasticsearch 的常用术语查询、使用以及与查询类型的兼容性



我目前正在调查使用通用术语查询的使用,并且由于该文档有点缺乏(或者我不仅没有在这些问题上找到任何文档),因此我并不完全是确保某些操作是否与常见术语查询不相容,或者我做错了。

我目前在Ubuntu 12.04,64位的Elasticsearch版本上。

这是我要观察的:

  • 查询类型匹配和match_phrase似乎与使用high_freq_operator,low_freq_operator和minimum_should_match选项不兼容。(例如[match] query does not support [high_freq_operator]和类似)

  • and,或者,或者不(复合表达式)在其组件表达式指定使用常见术语时似乎会产生损坏的基础表达式。(例如[_na] filter malformed, must start with start_object

  • span_term查询似乎与常见术语查询不相容。(例如[span_term] query does not support [common]

我的查询看起来很:

以下解析...

{   "query": {                                                                                                                                                                                                                              
        "match_phrase": {                                                                                                                                                                                                                   
            "subject": {                                                                                                                                                                                                                    
                "common": {                                                                                                                                                                                                                 
                    "body": {                                                                                                                                                                                                               
                        "cutoff_frequency": 0.001,                                                                                                                                                                                          
                        "query": "something not important"                                                                                                                                                                                  
                    }                                                                                                                                                                                                                       
                }                                                                                                                                                                                                                           
            }                                                                                                                                                                                                                               
        }                                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                                       
} 

这个人无法解析,引用" [匹配]查询不支持[high_freq_operator]:

{"query": {                                                                                                                                                                                                                              
        "match_phrase": {                                                                                                                                                                                                                   
            "subject": {                                                                                                                                                                                                                    
                "common": {                                                                                                                                                                                                                 
                    "body": {                                                                                                                                                                                                               
                        "cutoff_frequency": 0.001,
                        "high_freq_operator": "or",                                                                                                                                                                                          
                        "query": "something not important"                                                                                                                                                                                  
                    }                                                                                                                                                                                                                       
                }                                                                                                                                                                                                                           
            }                                                                                                                                                                                                                               
        }                                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                                       
} 

这个人未能解析,引用"过滤畸形,必须以start_object开头":

{                                                                                                                                                                                                                                           
    "filter": {                                                                                                                                                                                                                             
        "or": [                                                                                                                                                                                                                             
            {                                                                                                                                                                                                                               
                "query": {                                                                                                                                                                                                                  
                    "match": {                                                                                                                                                                                                              
                        "subject": {                                                                                                                                                                                                  
                            "common": {                                                                                                                                                                                                     
                                "body": {                                                                                                                                                                                                   
                                    "cutoff_frequency": 0.001,                                                                                                                                                                              
                                    "query": "PLEASE READ: something not important"                                                                                                                                                         
                                }                                                                                                                                                                                                           
                            }                                                                                                                                                                                                               
                        }                                                                                                                                                                                                                   
                    }                                                                                                                                                                                                                       
                }                                                                                                                                                                                                                           
            },                                                                                                                                                                                                                              
            {                                                                                                                                                                                                                               
                "query": {                                                                                                                                                                                                                  
                    "range": {                                                                                                                                                                                                              
                        "date": {                                                                                                                                                                                                           
                            "to": "2009-12-31T23:59:59Z"                                                                                                                                                                                    
                        }                                                                                                                                                                                                                   
                    }                                                                                                                                                                                                                       
                }                                                                                                                                                                                                                           
            }                                                                                                                                                                                                                               
        ]                                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                                       
} 

您误解了查询的结构。查询可以是" Leaf"查询(直接处理单个字段或字段),也可以是包装其他查询的"化合物"查询,例如booldis_max查询。

common –terms查询本身就是叶子查询,就像 matchmatch_phrasetermrange查询一样。您无法将common查询嵌入另一个叶子查询中。

match查询(不是match_phrasematch_phrase_prefix)已与common -TERMS查询部分集成,因为它支持cutoff_frequency参数。这是一个简单的集成:如果指定cutoff_frequency,则match查询在内部重写为common查询。如果您想要common terms的全部功能,则需要直接使用它。

所以此match查询:

{
   "query": {
      "match": {
         "subject": {
            "query": "some words to query",
            "cutoff_frequency": 0.001
         }
      }
   }
}

等效于此common查询:

{
   "query": {
      "common": {
         "subject": {
            "query": "some words to query",
            "cutoff_frequency": 0.001
         }
      }
   }
}

区别在于,在common查询中,还有许多其他旋钮可以扭曲,例如 high_freq_operator etc

for Elasticsearch 6.4

例如,加入 common with filter 查询使用以下方式:

{
  "explain": true,
  "query": {
    "bool": {
      "must": {
        "common": {
          "task_content": {
            "query": "this is bonsai cool superb",
            "cutoff_frequency": 0.01,
            "high_freq_operator": "and"
          }
        }
      },
      "filter": {
        "term": {
          "subject_id": 2
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "task_content": {}
    }
  }
}

希望它对新版本有帮助。

最新更新