Elasticsearch 查询语法更新 1.0.14 到 7.5.2

Elasticsearch query syntax update 1.0.14 to 7.5.2

我正在尝试更新一个相当大的 Elasticsearch 查询。我是 Elasticsearch 的新手,很难理解这里发生的一切。

这是原始查询:

elasticsearch_query search_models, {
  query: {
    filtered: {
      query: {
        function_score: {
          query: {
            bool: {
              must: [
                {
                  multi_match: {
                    operator: "and",
                    type: "cross_fields",
                    query: params[:term],
                    fuzziness: (params[:fuzzy] || 0),
                    fields: [
                      "name^2", "address", "email", "email2",
                      "primary_contact", "id", "lotname^7",
                      "lotname_keyword^9", "corp_name^4",
                      "vin^4", "serial_number^3", "dba_names",
                      "title_number", "title_tracking",
                      "full_name^3", "username", "reference", "user_name",
                      "phone", "text_number", "name_keyword^9"
                    ],
                  },
                }
              ],
              must_not: [
                { term: { access: { value: 2 } } },
                { term: { ledger: { value: "payroll" } } },
                { term: { ledger: { value: "credit card" } } }

              ],
              should: [
                { term: { active: { value: 1, boost: 100 } } },
                { term: { active: { value: 2, boost: 50 } } },
                { term: { active: { value: 3, boost: 0.05 } } },
                { term: { active: { value: 4, boost: 50 } } },
                { term: { active: { value: 5, boost: 50 } } },
                { term: { active: { value: 6, boost: 50 } } },
                { term: { branch_id: { value: current_branch.id, boost: 100 } } }
              ]
            }
          },
          functions: [
            {
              filter: { term: { auto_declined: 1 } },
              boost_factor: 0.3
            },
            {
              filter: { term: { auto_declined: 0 } },
              boost_factor: 0.0001
            },
            {
              filter: { term: { access: 1 } }, # current employee
              boost_factor: 10
            },
            {
              filter: { term: { access: 0 } },
              boost_factor: 0.2
            },
            {
              filter: { term: { unit_status: 1 } }, # current unit
              boost_factor: 2
            },
            {
              filter: { type: {value: 'txn'} },
              boost_factor: 0.4
            }
          ]
        }
      },
      filter: {
        "or" =>
        { filters: [
          { term: { "branch_id" => current_branch.id }},
          { type: { "value" => "auction" }},
          { type: { "value" => "fee_schedule"}},
          { type: { "value" => "unit"}},
          {
            "and" => [
              { type: { "value" => "user" }},
              { "or" => [
                { term: { "access" => 1 }}
              ]}
            ]
          }
        ]}
      }
    }
  }
}

这是我到目前为止的进展:

elasticsearch_query search_models, {
  query: {
    bool: {
      must: {
        function_score: {
          query: {
            bool: [
              must: {
                multi_match: {
                  query: params[:term],
                  type: "cross_fields",
                  operator: "and",
                  fields: [
                    "name^2", "address", "email", "email2",
                    "primary_contact", "id", "lotname^7",
                    "lotname_keyword^9", "corp_name^4",
                    "vin^4", "serial_number^3", "dba_names",
                    "title_number", "title_tracking",
                    "full_name^3", "username", "reference", "user_name",
                    "phone", "text_number", "name_keyword^9"
                  ]
                }
              },
              must_not: [
                { term: { access: { value: 2 } } },
                { term: { ledger: { value: "payroll" } } },
                { term: { ledger: { value: "credit card" } } }
              ],
              should: [
                { term: { active: { value: 1, boost: 100 } } },
                { term: { active: { value: 2, boost: 50 } } },
                { term: { active: { value: 3, boost: 0.05 } } },
                { term: { active: { value: 4, boost: 50 } } },
                { term: { active: { value: 5, boost: 50 } } },
                { term: { active: { value: 6, boost: 50 } } },
                { term: { branch_id: { value: current_branch.id, boost: 100 } } }
              ]
            ]
          },
          functions: [
            {
              filter: { term: { auto_declined: 1 } },
              weight: 0.3
            },
            {
              filter: { term: { auto_declined: 0 } },
              weight: 0.0001
            },
            {
              filter: { term: { access: 1 } }, # current employee
              weight: 10
            },
            {
              filter: { term: { access: 0 } },
              weight: 0.2
            },
            {
              filter: { term: { unit_status: 1 } }, # current unit
              weight: 2
            },
            {
              filter: { type: {value: 'txn'} },
              weight: 0.4
            }
          ]
        }
      },
      filter: [
        { term: { branch_id: current_branch.id } },
        { type: { value: "auction" } },
        { type: { value: "fee_schedule"} },
        { type: { value: "unit"} },
        bool: {
          must: {
            bool: {
              should: [
                { type: { value: "user" } },
                { term: { access: 1 } }
              ]
            }
          }
        }
      ]
    }
  }
}

我有:

前三个操作似乎已经解决了各自的错误,但我被这个过滤器挂断了 'or' 和 'and' 逻辑。非常感谢您的指导!

这是我收到的错误:

[400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] query malformed, no start_object after query name","line":1,"col":61}],"type":"parsing_exception","reason":"[bool] query malformed, no start_object after query name","line":1,"col":61},"status":400}

如果有任何进一步的信息对您有帮助,请告诉我。

您的查询正在形成无效 Json,这就是异常的原因,请参阅下面没有 Json 错误的查询。

{
  "query": {
    "bool": {
      "must": {
        "function_score": {
          "query": {
            "bool": {
              "must": [{
                "multi_match": {
                  "query": "params[:term]",
                  "type": "cross_fields",
                  "operator": "and",
                  "fields": [
                    "name^2", "address", "email", "email2",
                    "primary_contact", "id", "lotname^7",
                    "lotname_keyword^9", "corp_name^4",
                    "vin^4", "serial_number^3", "dba_names",
                    "title_number", "title_tracking",
                    "full_name^3", "username", "reference", "user_name",
                    "phone", "text_number", "name_keyword^9"
                  ]
                }
              }],
              "must_not": [
                { "term": { "access": { "value": 2 } } },
                { "term": { "ledger": { "value": "payroll" } } },
                { "term": { "ledger": { "value": "credit card" } } }
              ],
              "should": [
                { "term": { "active": { "value": 1, "boost": 100 } } }
              ]
            }
          },
          "functions": [
            {
              "filter": { "term": { "auto_declined": 1 } },
              "weight": 0.3
            },
            {
              "filter": { "term": { "auto_declined": 0 } },
               "weight": 0.0001
            },
            {
              "filter": { "term": { "access": 1 } },
               "weight": 10
            },
            {
              "filter": { "term": { "access": 0 } },
               "weight": 0.2
            },
            {
              "filter": { "term": { "unit_status": 1 } },
               "weight": 2
            },
            {
              "filter": { "type": {"value": "txn"} },
               "weight": 0.4
            }
          ]
        }
      },
      "filter": [
        { "term": { "branch_id": "current_branch.id" } },
        { "type": { "value": "auction" } },
        { "type": { "value": "fee_schedule"} },
        { "type": { "value": "unit"} },
        {"bool": {
          "must": {
            "bool": {
              "should": [
                { "type": { "value": "user" } },
                { "term": { "access": 1 } }
              ]
            }
          }
        }
        }
      ]
    }
  }
}

花括号和括号可以对比修改