【问题标题】:How to Create complex search Query for ElasticSearch?如何为 ElasticSearch 创建复杂的搜索查询?
【发布时间】:2017-10-24 11:58:18
【问题描述】:

我已经使用以下映射创建了 ElasticSearch 索引。 我需要编写复杂的搜索查询,例如在查询结果中查找相关项目。 例如,查找价格为“100”的项目。从搜索结果中获取 ItemBrand,然后找到具有相同 ItemBrand 的其他项目。(类似产品)。 谁能帮我吗?

PUT myindex-solvisoft2
{
"mappings": {
"ItemCategory":{
"properties": {Category":{"type": "text"},"IsActive":{"type": "boolean"},"Comment":{"type": "text" }}
},

"ItemBrand": {
"properties": {"Creator": {"type": "keyword"},"DateEffective": {"type": "date"},"DateTerminated": {"type": "date"},"Description": {"type": "text"},
"IsActive": {"type": "boolean"},"ItemBrand": {"type": "keyword"}}
},

"Item": {
"properties": {
"Comment": {"type": "text"},"Creator": {"type": "keyword"},"DateEffective": {"type": "date"},
"DateTerminated": {"type": "date"},"IsActive": {"type": "boolean"},"ItemBrand": {"type": "keyword"   },
"ItemDescription": { "type": "text"  },"ItemCategory": {"type": "keyword"},"ItemId": {"type": "integer" },
"Price": {"type": "double"  }    }
}
}
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在不运行的情况下编写查询将非常具有挑战性,但我可以分解这些步骤。

    使用布尔必须,找出价格等于100的记录。

    之后,您需要过滤掉查询并使用字段搜索具有相同商品品牌的商品。

    没那么复杂。

    【讨论】:

    • 我不想要搜索结果中的项目。单独搜索应在第一个 Result.ItemBrand 的基础上启动。
    • 类似这样的 => 从 ItemBrand 所在的项目中选择 *(从价格 = 100 的项目中选择项目品牌)
    【解决方案2】:

    这是我针对各个问题的解决方案。如果需要的话,对初学者有帮助。

    GET myindex-solvisoft_similar_products/_search
    {
    "query": {
    "bool": {
    "filter": {
    "term": {
    "brand": "Microsoft"
    }
    }
    }
    },
    "aggs": {
    "colors": {
    "terms": {
    "field": "color"
    }
    },
    "color_red": {
    "filter": {
    "term": {
    "color": "red"
    }
    },
    "aggs": {
    "models": {
    "terms": {
    "field": "model"
    }
    }
    }
    }
    },
    "post_filter": {
    "term": {
    "color": "red"
    }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多