2

我有这种情况,其中在同一个查询中有两个 multi_match 搜索。问题是,当我在 ruby​​ 中为其创建 JSON 时,似乎不可能使用具有非唯一键的 json,因此只出现其中一个键。

这是我的查询:

{
    "fields": ["id", "title", 
    "address.city", "address.state", "address.country", "address.state_code", "address.country_code", "proxy_titles", "location"],
    "size":2,
    "query":{
        "filtered":{
            "filter": {
                "range": {
                    "custom_score": {
                        "gte": 100
                    }
                }
            },
            "query":{
                "bool": {
                    "must": {
                        "multi_match":{
                            "query": "term 1",
                            "type": "cross_fields",
                            "fields": ["title^2", "proxy_titles^2","description"]
                        }
                    },
                    "must": {
                        "multi_match": {
                            "query": "us",
                            "fields": ["address.city", "address.country", "address.state",
                            "address.zone", "address.country_code", "address.state_code", "address.zone_code"]
                        }
                    }
                }
            }
        }
    },
    "sort": {
        "_score": { "order": "desc" },
        "variation": {"order": "asc"},
        "updated_at": { "order": "desc" }
    }
}

我也是最近才开始使用弹性搜索,所以如果你能建议我一个更好的查询来完成同样的事情,那将非常有帮助。

4

1 回答 1

1

你有语法错误。对于 a 中的多个"must""bool",它们需要在一个数组中。不幸的是,文档并不总是非常有用(bool 查询页面显示此为"should"但不是"must")。

尝试这个:

{
   "fields": ["id","title","address.city","address.state","address.country","address.state_code","address.country_code","proxy_titles","location"],
   "size": 2,
   "query": {
      "filtered": {
         "filter": {
            "range": {
               "custom_score": {
                  "gte": 100
               }
            }
         },
         "query": {
            "bool": [
               {
                  "must": {
                     "multi_match": {
                        "query": "term 1",
                        "type": "cross_fields",
                        "fields": ["title^2","proxy_titles^2","description"]
                     }
                  }
               },
               {
                  "must": {
                     "multi_match": {
                        "query": "us",
                        "fields": ["address.city","address.country","address.state","address.zone","address.country_code","address.state_code","address.zone_code"]
                     }
                  }
               }
            ]
         }
      }
   },
   "sort": {
      "_score": {
         "order": "desc"
      },
      "variation": {
         "order": "asc"
      },
      "updated_at": {
         "order": "desc"
      }
   }
}
于 2015-09-19T22:57:07.287 回答