elasticsearch - Elastic Search 搜索查询返回不正确的结果

标签 elasticsearch

我正在对 ElasticSearch 索引执行搜索查询,结果很奇怪。我想找到 product.id = 209349 所在的所有文档:

{
  "index": "products",
  "from": 0,
  "size": 100,
  "body": {
    "query": {
      "filtered": {
        "filter": [
          {
            "term": {
              "product.id": 209349
            }
          }
        ]
      }
    }
  }
}

但是,结果返回给我一个文档,其中 product.id = 83875 .我注意到的是 product.variant.id等于 209349 ...这是怎么回事?

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "products",
        "_type": "product",
        "_id": "UPC-83875",
        "_score": 1,
        "_source": {
          "mpn": "UPC-83875",
          "product_count": 1,
          "price": "448.00",
          "price_discount_amount": null,
          "product": [
            {
              "id": 83875,
              "posted_on": "2014-07-23 22:08:36",
              "status_id": 3,
              "sku": "23469984",
              "mpn": "UPC-83875",
              "name": "Laser Toner Cartridge Set Black Cyan Yellow Magenta",
              "description": "",
              "has_image": true,
              "currency_id": 1,
              "price": "448.00",
              "variant": [
                {
                  "id": 209349,
                  "sku": "23469984",
                  "name": null,
                  "price": "448.00",
                  "discount_amount": null,
                  "price_total": "448.00",
                  "has_image": false
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

这是架构:

{
  "dynamic": "strict",
  "properties": {
    "mpn": {
      "type": "string",
      "index": "not_analyzed"
    },
    "price": {
      "type": "double"
    },
    "price_discount_amount": {
      "type": "double"
    },
    "product": {
      "properties": {
        "id": {
          "type": "long"
        },
        "posted_on": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "status_id": {
          "type": "long"
        },
        "sku": {
          "type": "string",
          "index": "not_analyzed"
        },
        "mpn": {
          "type": "string",
          "index": "not_analyzed"
        },
        "name": {
          "type": "string",
          "analyzer": "english"
        },
        "description": {
          "type": "string",
          "analyzer": "english"
        },
        "has_image": {
          "type": "boolean"
        },
        "price": {
          "type": "double"
        },
        "price_discount_amount": {
          "type": "double"
        },
        "currency_id": {
          "type": "long"
        },
        "variant": {
          "properties": {
            "id": {
              "type": "long"
            },
            "sku": {
              "type": "string",
              "index": "not_analyzed"
            },
            "name": {
              "type": "string",
              "analyzer": "english"
            },
            "discount_amount": {
              "type": "double"
            },
            "price": {
              "type": "double"
            },
            "price_total": {
              "type": "double"
            },
            "has_image": {
              "type": "boolean"
            }
          }
        }
      }
    }
  }
}

编辑/解决:问题似乎是我的索引 _type被命名为product以及内部对象。所以发生的事情是 ES 匹配 <_type>.id (有点像做 *.id )它会匹配 variant.id因为它是 id字段。

使用的正确名称显然是 product.product.id最终成为完整路径。

这种行为显然是为了安抚很久以前的一些用户:https://github.com/elastic/elasticsearch/issues/3005

最佳答案

我不确定你为什么会得到那个结果(当我尝试它时它没有返回),但你可能想尝试这样的映射,使用 nested types :

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "mpn": {
               "type": "string"
            },
            "price": {
               "type": "string"
            },
            "product_count": {
               "type": "long"
            },
            "product": {
               "type": "nested",
               "properties": {
                  "currency_id": {
                     "type": "long"
                  },
                  "description": {
                     "type": "string"
                  },
                  "has_image": {
                     "type": "boolean"
                  },
                  "id": {
                     "type": "long"
                  },
                  "mpn": {
                     "type": "string"
                  },
                  "name": {
                     "type": "string"
                  },
                  "posted_on": {
                     "type": "string"
                  },
                  "price": {
                     "type": "string"
                  },
                  "sku": {
                     "type": "string"
                  },
                  "status_id": {
                     "type": "long"
                  },
                  "variant": {
                     "type": "nested",
                     "properties": {
                        "has_image": {
                           "type": "boolean"
                        },
                        "id": {
                           "type": "long"
                        },
                        "price": {
                           "type": "string"
                        },
                        "price_total": {
                           "type": "string"
                        },
                        "sku": {
                           "type": "string"
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

当我使用此映射创建索引并为您的文档编制索引时:

PUT /test_index/doc/UPC-83875
{
   "mpn": "UPC-83875",
   "product_count": 1,
   "price": "448.00",
   "price_discount_amount": null,
   "product": [
      {
         "id": 83875,
         "posted_on": "2014-07-23 22:08:36",
         "status_id": 3,
         "sku": "23469984",
         "mpn": "UPC-83875",
         "name": "Laser Toner Cartridge Set Black Cyan Yellow Magenta",
         "description": "",
         "has_image": true,
         "currency_id": 1,
         "price": "448.00",
         "variant": [
            {
               "id": 209349,
               "sku": "23469984",
               "name": null,
               "price": "448.00",
               "discount_amount": null,
               "price_total": "448.00",
               "has_image": false
            }
         ]
      }
   ]
}

然后用了一个nested filter在我的查询中,它没有返回:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "product",
               "filter": {
                  "term": {
                     "product.id": 209349
                  }
               }
            }
         }
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

但是,它由这两个查询返回:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "product",
               "filter": {
                  "term": {
                     "product.id": 83875
                  }
               }
            }
         }
      }
   }
}

还有这个:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "product.variant",
               "filter": {
                  "term": {
                     "product.variant.id": 209349
                  }
               }
            }
         }
      }
   }
}

这是我用来测试的一些代码:

http://sense.qbox.io/gist/8f5fa6f2ced088a42b92f495c0668024b9ef19c8

关于elasticsearch - Elastic Search 搜索查询返回不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29957859/

相关文章:

c# - ElasticSearch:检索给定时间戳的最新文档

elasticsearch - 用于配置 Elasticsearch 的 AWS Cloudformation 模板

java - Eclipse J2E : IndexNotFoundException[no such index]

elasticsearch - Elasticsearch 中的内部对象与反规范化

elasticsearch - ElasticSearch中的BoolFilter和BoolQuery

python - 搜索多个模型 (Elasticsearch)

python - Django 没有名为 elasticsearch_dsl.connections 的模块

elasticsearch - 小型ES集群卡在initializing_shards上

elasticsearch - logstash kv {}过滤器插件如何工作?

python - 如何使用 python 更新 Elastic Search ( Kibana ) 中可用的大量 Json 数据