elasticsearch - 如何为此编写 Elasticsearch 查询?

标签 elasticsearch elasticsearch-dsl elasticsearch-query

我们如何在以下条件下编写查询:

  • 状态不得为“无效”
  • searchString必须为“some_String”
  • 购买的必须是真实的
  • 在productCost
  • 下必须具有“产品1”
  • 根据“产品1”的成本对它们进行排序

  • 数据结构:
    “name”:“Ross”,
    “status”:“ACTIVE”,
    “adsPurchased”:是的,
    “searchString”:“some_tring”,
    “productCost”:[
    {
    “产品”:“产品1”,
    “费用”:“50.0”
    },
    {
    “产品”:“产品2”,
    “费用”:“80.0”
    }
    ]
    }

    {
    “name”:“Chandler”,
    “status”:“INACTIVE”,
    “adsPurchased”:是的,
    “searchString”:“some_String”,
    “productCost”:[
    {
    “产品”:“产品1”,
    “费用”:“60.0”
    },
    {
    “产品”:“产品4”,
    “费用”:“800.0”
    }
    ]
    }

    {
    “name”:“joey”,
    “status”:“ACTIVE”,
    “adsPurchased”:是的,
    “searchString”:“some_tring”,
    “productCost”:[
    {
    “产品”:“产品1”,
    “费用”:“30.0”
    },
    {
    “产品”:“产品5”,
    “费用”:“90.0”
    }
    ]
    }

    所以,我应该找罗斯和乔伊

    最佳答案

    添加带有映射,示例文档和搜索查询的工作示例。
    映射:

    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          },
          "status": {
            "type": "text"
          },
          "adsPurchased": {
            "type": "boolean"
          },
          "searchString": {
            "type": "text"
          },
          "productCost": {
            "type": "nested",
            "properties": {
              "product": {
                "type": "text"
              },
              "cost": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
    
    搜索查询:
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "adsPurchased": true    -->adsPurchased MUST to be true
              }
            },
            {
              "match": {
                "searchString": "some_tring"    -->searchString MUST be "some_tring"
              }
            },
            {
              "nested": {
                "path": "productCost",
                "query": {
                  "bool": {
                    "must": [           -->must have "Product 1" under productCost
                      {
                        "match": {
                          "productCost.product": "Product 1"  
                        }
                      }
                    ]
                  }
                }
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "status": "INACTIVE"    -->status MUST NOT be "INACTIVE"              
                }
            }
          ]
        }
      },
      "sort": [                                 -->Sort them based on cost
        {
          "productCost.cost": {
            "order": "asc",
            "nested_path": "productCost"
          }
        }
      ]
    }
    
    搜索结果:
    "hits": [
          {
            "_index": "foo3",
            "_type": "_doc",
            "_id": "2",
            "_score": null,
            "_source": {
              "name": "joey",
              "status": "ACTIVE",
              "adsPurchased": true,
              "searchString": "some_tring",
              "productCost": [
                {
                  "product": "Product 1",
                  "cost": "30.0"
                },
                {
                  "product": "Product 5",
                  "cost": "90.0"
                }
              ]
            },
            "sort": [
              30
            ]
          },
          {
            "_index": "foo3",
            "_type": "_doc",
            "_id": "1",
            "_score": null,
            "_source": {
              "name": "Ross",
              "status": "ACTIVE",
              "adsPurchased": true,
              "searchString": "some_tring",
              "productCost": [
                {
                  "product": "Product 1",
                  "cost": "50.0"
                },
                {
                  "product": "Product 2",
                  "cost": "80.0"
                }
              ]
            },
            "sort": [
              50
            ]
          }
        ]
    
    在搜索结果中,您将得到所需的结果,即Ross和joey
    要了解有关嵌套排序的更多信息,可以引用此官方的documentation;有关嵌套查询的信息,请引用this

    关于elasticsearch - 如何为此编写 Elasticsearch 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62495447/

    相关文章:

    azure - virto commerce 是否支持多个 Elastic Search Worker 角色,并共享索引数据

    elasticsearch - 在Kibana 4中显示多行

    c# - Geo_Point 属性未按预期编制索引

    docker - MacOS 14 上 docker 镜像中的 Elasticsearch vm.max_map_count 错误

    elasticsearch - 需要从 Elasticsearch 上的 AND、OR 过滤器升级为 ES7 的 bool 查询

    php - Yii2(或独立)中用于ElasticSearch查询DSL的构建器

    java - Elasticsearch默认不考虑字符串字段并且不给出正确的匹配结果

    Elasticsearch 聚合 : Sum of latest childrens per parent

    elasticsearch - Elasticsearch为什么要为词条查询计算分数?

    elasticsearch - 如何使用Elasticsearch方面查询对结果进行分组