簡體   English   中英

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 } }
              ]
            }
          }
        }
      ]
    }
  }
}

我有:

  • 將“過濾”替換為“布爾”和“必須”
  • 將“boost_factor”替換為“weight”
  • 從“cross_fields”類型“multi_match”中刪除了“fuzziness”
  • 試圖用更新的“bool”語法更新“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 } }
              ]
            }
          }
        }
        }
      ]
    }
  }
}

您可以比較和修改花括號和方括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM