Laravel 如何在elasticsearch中使用多位置条件
我正在使用laravel+elasticsearch。 我有这样一个数组:Laravel 如何在elasticsearch中使用多位置条件,laravel,
elasticsearch,Laravel,
elasticsearch,我正在使用laravel+elasticsearch。 我有这样一个数组: [ { "title": "product_title", "stocks": [ { "country": "EN", "stock": 0 },
[
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
},
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
]
现在我想找到所有对象的国家
相等EN
,并且股票
大于1
已更新 我的问题是:
{
"index": "products",
"body": {
"size": 15,
"from": 1,
"sort": [
{
"stock": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "**",
"type": "best_fields",
"fields": [
"erp_id",
"title_en^2",
"translations.title^2",
"erp.title_en",
"erp.title",
"erp.options.title",
"erp.options.title_en"
],
"analyze_wildcard": true,
"allow_leading_wildcard": true
}
}
],
"filter": [
{
"term": {
"is_active": 1
}
},
{
"term": {
"shops.shop_id": 1
}
}
]
}
},
"aggs": {
"max_price": {
"filter": {
"term": {
"erp.price_lists.currency.abbr": "tmn"
}
},
"aggs": {
"result": {
"max": {
"field": "erp.price_lists.pivot.price_tt"
}
}
}
},
"min_price": {
"filter": {
"term": {
"erp.price_lists.currency.abbr": "tmn"
}
},
"aggs": {
"result": {
"min": {
"field": "erp.price_lists.pivot.price_tt"
}
}
}
}
}
}
}
您可以使用with获得满足要求的对象
添加工作示例
索引映射:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
索引数据:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
搜索查询:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
搜索结果:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
您可以使用with获得满足要求的对象
添加工作示例
索引映射:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
索引数据:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
搜索查询:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
搜索结果:
{
"mappings": {
"properties": {
"stocks": {
"type": "nested"
}
}
}
}
{
"title": "product_title_2",
"stocks": [
{
"country": "EN",
"stock": 1
},
{
"country": "IN",
"stock": 0
}
]
}
{
"title": "product_title",
"stocks": [
{
"country": "EN",
"stock": 0
},
{
"country": "IN",
"stock": 1
}
]
}
{
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
}
{
"query": {
"nested": {
"path": "stocks",
"query": {
"bool": {
"filter": [
{
"match": {
"stocks.country": "EN"
}
},
{
"range": {
"stocks.stock": {
"gt": 1
}
}
}
]
}
},
"inner_hits":{}
}
}
}
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"title": "product_title_3",
"stocks": [
{
"country": "EN",
"stock": 2
},
{
"country": "IN",
"stock": 0
}
]
},
"inner_hits": {
"stocks": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "67294405",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "stocks",
"offset": 0
},
"_score": 0.0,
"_source": {
"country": "EN",
"stock": 2
}
}
]
}
}
}
}
]
你是想在ElastSearch还是PHP中搜索这个表达式?@enriqueojedalara更新了我的问题。添加了我的查询。您是在ElastSearch中还是在PHP中搜索该表达式?@enriqueojedalara更新了我的问题。添加了我的查询。考虑使用<代码> Boo/Field而不是<代码> Boo/必须进行精确匹配,在查询中没有<代码>内射命中< /代码>节。<代码>必须和<代码>过滤器< /代码>是<代码>过滤器< /代码>不影响评分,虽然
必须
这样做,但不一定是精确匹配。在这种情况下,您可能(也可能不)希望两者都匹配的项目得分高于两个条件之一匹配的项目。因为这个问题还不清楚,我认为必须和过滤器应该是一样的valid@ESCoder更新了我的问题。添加了我的查询。考虑使用<代码> Boo/Field而不是<代码> Boo/必须进行精确匹配,在查询中没有<代码>内射命中< /代码>节。<代码>必须和<代码>过滤器< /代码>是<代码>过滤器< /代码>不影响评分,虽然必须
这样做,但不一定是精确匹配。在这种情况下,您可能(也可能不)希望两者都匹配的项目得分高于两个条件之一匹配的项目。因为这个问题还不清楚,我认为必须和过滤器应该是一样的valid@ESCoder更新了我的问题。添加了我的查询。