弹性搜索多个字段过滤

时间:2017-12-26 03:38:37

标签: elasticsearch

说到弹性搜索,我是新手。这就是为什么,我的问题是,如果我想过滤多个字段,聚合可能会解决问题吗?或者我应该使用什么?

{
  "aggs": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
           { "term" : {"shop_slug" : "sharkys-dhammazedi.6326"}}, 
                    { "term" : 
{"slug" : "anchovy-essence"}}

          ]
        }
      }
 }
}}

这是映射

"product_catalogue": {
"mappings": {
"products": {
"properties": {
"buy_online": {
"type": "long"
},
"category": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"slug": {
"type": "string"
},
"tree": {
"properties": {
"2": {
"type": "string"
},
"3": {
"type": "string"
},
"5": {
"type": "string"
},
"10": {
"type": "string"
},
"11": {
"type": "string"
},
"12": {
"type": "string"
},
"13": {
"type": "string"
},
"14": {
"type": "string"
},
"15": {
"type": "string"
},
"16": {
"type": "string"
},
"17": {
"type": "string"
},
"18": {
"type": "string"
},
"19": {
"type": "string"
},
"20": {
"type": "string"
},
"21": {
"type": "string"
},
"22": {
"type": "string"
},
"23": {
"type": "string"
},
"24": {
"type": "string"
},
"25": {
"type": "string"
},
"26": {
"type": "string"
},
"27": {
"type": "string"
},
"28": {
"type": "string"
},
"29": {
"type": "string"
},
"30": {
"type": "string"
},
"31": {
"type": "string"
},
"32": {
"type": "string"
},
"33": {
"type": "string"
},
"34": {
"type": "string"
},
"35": {
"type": "string"
},
"36": {
"type": "string"
},
"37": {
"type": "string"
},
"38": {
"type": "string"
},
"39": {
"type": "string"
},
"40": {
"type": "string"
},
"41": {
"type": "string"
},
"42": {
"type": "string"
},
"43": {
"type": "string"
},
"46": {
"type": "string"
},
"62": {
"type": "string"
},
"72": {
"type": "string"
},
"73": {
"type": "string"
},
"74": {
"type": "string"
},
"75": {
"type": "string"
},
"77": {
"type": "string"
},
"137": {
"type": "string"
},
"139": {
"type": "string"
},
"140": {
"type": "string"
},
"141": {
"type": "string"
}
}
}
}
},
"click_collect": {
"type": "long"
},
"company_id": {
"type": "string"
},
"company_name": {
"type": "string"
},
"company_slug": {
"type": "string",
"index": "not_analyzed"
},
"condition": {
"type": "string"
},
"created_at": {
"type": "date",
"format": "dateOptionalTime"
},
"currency_iso": {
"type": "string"
},
"delivery_available": {
"type": "boolean"
},
"delivery_type": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"pivot": {
"properties": {
"deliverytype_id": {
"type": "long"
},
"pricing": {
"type": "string"
},
"shop_id": {
"type": "long"
}
}
}
}
},
"description": {
"type": "string",
"analyzer": "snowball"
},
"group_id": {
"type": "string"
},
"image_url": {
"type": "string"
},
"listed_shop": {
"type": "integer"
},
"location": {
"type": "geo_point"
},
"mainPhoto": {
"type": "string"
},
"mm_description": {
"type": "string"
},
"mm_title": {
"type": "string"
},
"postcode": {
"type": "string"
},
"prices": {
"properties": {
"retail": {
"properties": {
"converted": {
"properties": {
"currencyIso": {
"type": "string"
},
"decimal": {
"type": "string"
},
"formatted": {
"type": "string"
}
}
},
"original": {
"properties": {
"currencyIso": {
"type": "string"
},
"decimal": {
"type": "string"
},
"formatted": {
"type": "string"
}
}
}
}
}
}
},
"shop_category": {
"type": "long"
},
"shop_id": {
"type": "long"
},
"shop_name": {
"type": "string"
},
"shop_slug": {
"type": "string",
"index": "not_analyzed"
},
"shop_stock_count": {
"type": "integer"
},
"shop_type": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
},
"slug": {
"type": "string"
},
"title": {
"type": "string",
"analyzer": "snowball"
},
"user_stock_count": {
"type": "integer"
},
"variant": {
"properties": {
"Colour": {
"type": "string"
},
"Disk Space": {
"type": "string"
},
"Length": {
"type": "string"
}
}
}
}
}
}
}

此查询似乎没有成功。事实上,它只过滤其中一个字段但无法解析来过滤shop_slug和slug这两个字段。请好好回答谢谢。

1 个答案:

答案 0 :(得分:-1)

如果要使用多字段进行过滤,最好在查询字段中使用filter子句,而不是使用聚合子句。

如果您对SQL数据库有一定的经验,那么filer子句就像SQL语言中的“where”子句。当你想要“select * from table where condition1 and condition2”时,你使用“filter”:[condition1_clause,condition2_clause]。 []中的所有条款都被视为“和”关系。

在你的情况下,如果目标是选择那些“shop_slug”是“sharkys-dhammazedi.6326”并且“slug”是“anchovy-essence”的那些,那么查询正文应该是

 @Entity
 @Table(name = "t_challenge_records", indexes = { @Index(columnList = 
  "USER"), @Index(columnList = "TYPE"),
    @Index(columnList = "STATUS"), @Index(columnList = "CREATED"), 
    @Index(columnList = "ORDERNO"),
    @Index(columnList = "CID"), @Index(columnList = "ISSUE"), 
    @Index(columnList = "MONEY"),
    @Index(columnList = "PROFIT"), @Index(columnList = "LATESTDATE") 
 })
 public class ChallengeRecord extends AbstractBaseEntity {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "ID")
 private Long id;
 private long cId;
 private String user;
 private String type;
 private String subType;;
 private int startDate;
 private int endDate;
 private int latestDate;
 private int latestTime;
 private int startTimeU;
 private int endTimeU;
 private int startTimeD;
 private int endTimeD;
 private String room; 
 private String issue; 
 private long money;
 private long profit;
 private String orderNO;
 private String reason;