弹性搜索不返回匹配的数据

时间:2017-10-10 04:26:31

标签: elasticsearch

我有一个弹性搜索索引,映射如下:

{
  "issues": {
    "mappings": {
      "issues": {
        "properties": {
          "captured_by": {
            "type": "long"
          },
          "captured_on": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "description": {
            "type": "string"
          },
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "updated_at": {
            "type": "date",
            "format": "dateOptionalTime"
          }
          "issue_org_states": {
            "type": "nested",
            "properties": {
              "assigned_at": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "assigned_by": {
                "type": "long"
              },
              "updated_at": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "updated_by": {
                "type": "long"
              }
            }
          }
        }
      }
    }
  }
}

填写一份文件如下:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "issues",
        "_type": "issues",
        "_id": "13f3bf09-08cb-4464-b326-15872bdb0870",
        "_score": 1,
        "_source": {
          "id": "13f3bf09-08cb-4464-b326-15872bdb0870",
          "description": "Sloppy paint job",
          "captured_on": "2017-10-09T09:24:01.928Z",
          "captured_by": 1,
          "updated_at": "2017-10-09T12:47:22.982Z",
          "issue_org_states": [
            {
              "updated_at": "2017-10-09T12:47:22.982Z",
              "updated_by": 1,
              "assigned_at": "2017-10-09T12:47:22.982Z",
              "assigned_by": 1879048240
            }
          ]
        }
      }
    ]
  }
}

我想在路径上查询" issues.issue_org_states"在属性assigned_at和updated_at。 查询如下:

{
  "query": {
    "nested": {
      "path": "issue_org_states",
      "range": {
        "assigned_at": {
          "from": "2017-10-09T00:00:00.000Z",
          "to": "2017-10-09T23:59:59.999Z",
          "include_lower": true,
          "include_upper": true
        }
      }
    }
  }
}

当我在上面运行查询它的返回数据时。但是如果在updated_at而不是assigned_at上运行相同的查询,则弹性搜索返回0结果。 assigned_at和updated_at都有相同的映射和相同的数据,直到查询下面没有返回任何结果。

    {
  "query": {
    "nested": {
      "path": "issue_org_states",
      "range": {
        "updated_at": {
          "from": "2017-10-09T00:00:00.000Z",
          "to": "2017-10-09T23:59:59.999Z",
          "include_lower": true,
          "include_upper": true
        }
      }
    }
  }
}

如果我在这里遗漏任何东西,请帮忙。

1 个答案:

答案 0 :(得分:0)

elasticsearch 5.4.0 上测试并查询 issue_org_states.updated_at issue_org_states.assigned_at 正常运行。您可能希望再次查看实际的命令行。

我的查询如下,基于this document

curl -XGET -u elastic:changeme localhost:9200/issues/issues/_search?pretty -d '{
  "query": {
    "nested": {
      "path": "issue_org_states",
      "query" : {
        "range": {
          "issue_org_states.updated_at": {
            "gte": "2017-10-09T00:00:00.000Z",
            "le": "2017-10-09T23:59:59.999Z"
          }
        }
      }
    }
  }
}'


curl -XGET -u elastic:changeme localhost:9200/issues/issues/_search?pretty -d '{
  "query": {
    "nested": {
      "path": "issue_org_states",
      "query" : {
        "range": {
          "issue_org_states.assigned_at": {
            "gte": "2017-10-09T00:00:00.000Z",
            "le": "2017-10-09T23:59:59.999Z"
          }
        }
      }
    }
  }
}'

映射定义,

curl -XPUT -u elastic:changeme localhost:9200/issues -d '{
    "mappings": {
      "issues": {
        "properties": {
          "captured_by": {
            "type": "long"
          },
          "captured_on": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "description": {
            "type": "string"
          },
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "updated_at": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "issue_org_states": {
            "type": "nested",
            "properties": {
              "assigned_at": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "assigned_by": {
                "type": "long"
              },
              "updated_at": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "updated_by": {
                "type": "long"
              }
            }
          }
        }
      }
    }
}'

和你一样的数据。

curl -XPOST -u elastic:changeme localhost:9200/issues/issues -d '{
"id": "13f3bf09-08cb-4464-b326-15872bdb0870",
"description": "Sloppy paint job",
"captured_on": "2017-10-09T09:24:01.928Z",
"captured_by": 1,
"updated_at": "2017-10-09T12:47:22.982Z",
"issue_org_states": [
            {
              "updated_at": "2017-10-09T12:47:22.982Z",
              "updated_by": 1,
              "assigned_at": "2017-10-09T12:47:22.982Z",
              "assigned_by": 1879048240
            }
]
}'