Elasticsearch查询关联返回无结果

时间:2015-05-01 18:41:33

标签: elasticsearch associations

我有一个属于类别模型的数据模型。因此,此数据模型与category_id列相关联。我的查询由ES处理,但没有返回任何结果。

我制作了一条记录,取了category_id,然后将category_id传递给下面的查询。我期望至少有一个结果,因为它是相同的category_id。但是,我得到0结果。

{
  query: {
    filtered: {
      query: {
        bool: {must: []},
      },
      filter: {
        bool: {
          must: {
            term: {category_id: #CATEGORY_ID PASSED IN HERE}
          }
        }
      }
    }
  }, 
  sort: [{
    _geo_distance: {
      store_location: {:lat=>0, :lon=>0},
      order: "asc",
      unit: "miles"
    }
  }]
}

这是我目前的映射。这里的属性可能无关紧要,因为它来自一个大型项目。

"test-somethings": {
  "mappings": {
    "something": {
      "dynamic": "false",
      "properties": {
        "bpid": {
          "type": "string"
        },
        "category_id": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "listings": {
          "type": "nested",
          "include_in_parent": true,
          "properties": {
            "store_id": {
              "type": "string"
            }
          }
        },
        "name": {
          "type": "string"
        },
        "store_id": {
          "type": "integer"
        },
        "store_location": {
          "type": "geo_point"
        }
      }  
    }
  }
}

我正在测试的一些数据:

[{
  "id":1,
  "bpid":"84202e8fc0",
  "name":"Something",
  "description":"Labore et odit quas sit similique. Molestiae deserunt rem harum assumenda voluptate placeat est. Reiciendis iusto ea et. Nesciunt qui et ab porro vitae ut. Exercitationem et dolores ullam.",
  "created_at":"2015-05-01T21:02:23.000Z",
  "updated_at":"2015-05-01T21:02:23.000Z",
  "category_id":1282
},
  "id":2,
  "bpid":"9719093ac5",
  "name":"Test Something",
  "description":"Dolores minima fugit sunt. Consequuntur aspernatur dolorem. Quasi dolorem natus occaecati eos culpa error. Voluptates dolores pariatur ab impedit minima cumque molestiae.",
  "created_at":"2015-05-01T21:02:23.000Z",
  "updated_at":"2015-05-01T21:02:23.000Z",
  "category_id":1283
},
  "id":3,
  "bpid":"5026503fc5",
  "name":"Pot",
  "description":"Et facilis et voluptatem amet. Quidem qui molestiae quia voluptas voluptatum. Facilis aut illo dicta provident animi omnis. Et quia dicta omnis aliquam quas sequi. Nemo sed officia consequatur excepturi.",
  "created_at":"2015-05-01T21:02:23.000Z",
  "updated_at":"2015-05-01T21:02:23.000Z",
  "category_id":1284
}]

1 个答案:

答案 0 :(得分:0)

试试这个:

{
  query: {
    filtered: {
      query: {
        match_all: {},
      },
      filter: {
        bool: {
          must: {
            term: {category_id: #CATEGORY_ID PASSED IN HERE}
          }
        }
      }
    }
  }, 
  sort: [{
    _geo_distance: {
      store_location: {:lat=>0, :lon=>0},
      order: "asc",
      unit: "miles"
    }
  }]
}

以下是我用来测试它的代码:

http://sense.qbox.io/gist/e538630b18e499a1bc566896413627b540aae745