English 中文(简体)
Elasticsearch中的多个必须条件
原标题:Multiple must conditions in Elasticsearch

我是Elasticsearch的初学者,我正在尝试使用span_near子句(搜索到的查询位于第一位的文档的得分必须高于搜索到的搜索位于第二位的文档)。我正在使用Elasticsearch 7。

我试过这个:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "query_string": {
                                    "query": "chaudiere",
                                    "default_operator": "OR",
                                    "analyzer": "french",
                                    "fields": [
                                        "name"
                                    ],
                                    "type": "cross_fields",
                                    "lenient": true,
                                    "fuzziness": "AUTO:10,20"
                                }
                            }
                        ]
                    },
                    "must": {
                        "span_near": {
                            "slop": 2,
                            "in_order": false,
                            "clauses": [
                                {
                                    "span_multi": {
                                        "match": {
                                            "fuzzy": {
                                                "label": {
                                                    "value": "chaudiere",
                                                    "fuzziness": "AUTO"
                                                }
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            ],
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "store_uid": 1
                                }
                            },
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "bool": {
                                                "must_not": [
                                                    {
                                                        "term": {
                                                            "life_cycle": "FDS"
                                                        }
                                                    }
                                                ]
                                            }
                                        },
                                        {
                                            "bool": {
                                                "must": [
                                                    {
                                                        "term": {
                                                            "life_cycle": "FDS"
                                                        }
                                                    },
                                                    {
                                                        "bool": {
                                                            "should": [
                                                                {
                                                                    "bool": {
                                                                        "must": [
                                                                            {
                                                                                "range": {
                                                                                    "stock": {
                                                                                        "gt": 0
                                                                                    }
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                },
                                                                {
                                                                    "bool": {
                                                                        "must": [
                                                                            {
                                                                                "range": {
                                                                                    "stock_delivery": {
                                                                                        "gt": 0
                                                                                    }
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                }
                                                            ]
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "term": {
                                    "is_base": true
                                }
                            },
                            {
                                "term": {
                                    "is_replaced": true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

但我有这个信息

[bool] malformed query, expected [END_OBJECT]

你知道为什么吗?

谢谢你的帮助

问题回答

错误是因为您设置的must子句不正确。不能在同一级别使用bool查询,必须使用。

查询出错:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "FIELD": "TEXT"
                }
              }
            ]
          },
          "must": {
            "match": {
              "FIELD": "TEXT"
            }
          }
        }
      ]
    }
  }
}

我想纠正一下:建议将bool查询中的query_string和span_near子句放在must中。

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": "chaudiere",
                  "default_operator": "OR",
                  "analyzer": "french",
                  "fields": [
                    "name"
                  ],
                  "type": "cross_fields",
                  "lenient": true,
                  "fuzziness": "AUTO:10,20"
                }
              },
              {
                "span_near": {
                  "slop": 2,
                  "in_order": false,
                  "clauses": [
                    {
                      "span_multi": {
                        "match": {
                          "fuzzy": {
                            "label": {
                              "value": "chaudiere",
                              "fuzziness": "AUTO"
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "store_uid": 1
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "term": {
                              "life_cycle": "FDS"
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "life_cycle": "FDS"
                            }
                          },
                          {
                            "bool": {
                              "should": [
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "range": {
                                          "stock": {
                                            "gt": 0
                                          }
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "range": {
                                          "stock_delivery": {
                                            "gt": 0
                                          }
                                        }
                                      }
                                    ]
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ],
            "must_not": [
              {
                "term": {
                  "is_base": true
                }
              },
              {
                "term": {
                  "is_replaced": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}




相关问题
Roll over index with elastic search and serilog

We are using es 6.7 and serilog 7.1 in our dotnet core application. In our logger implementation vi are using the following index "app-{0:yyyy.MM}-1" for our ElasticsearchSinkOptions. This ...

Change the date format in kibana

I am working for my internship on the implementation of the ElasticSearch family (ElasticSearch+ Kibana+ logstash). Here is my question: I have a field "@ timestamp" with the following format: 2014-05-...

热门标签