Elasticsearch嵌套内部命中查询缺少结果

4smxwvx5  于 6个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(64)

我想查询一个对象的内部命中。当我返回排序的,嵌套的,内部命中时,缺少一个。当我返回以不同方向排序的对象时,结果在那里,但另一个丢失(总是有4个结果中的3个存在)。
为什么我看不到所有结果?结果部分声称有4个对象,但只有3个存在。
这是我的索引:

PUT example_index/
{
    "mappings": {
        "properties": {
            "id": {
                "type": "long"
            },
            "conversation_events": {
                "type": "nested",
                "properties": {
                    "occurred_at": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time ||epoch_millis"
                    },
                    "type": {
                        "type": "keyword"
                    },
                    "message": {
                        "properties": {
                            "id": {
                                "type": "keyword"
                            }
                        }
                    },
                    "account_event": {
                        "properties": {
                            "id": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

字符串
一个对象(有4个嵌套对象):

POST /example_index/_doc/1
{
  "id": 1,
  "conversation_events": [
    {
      "occurred_at": "2023-01-01T12:00:00Z",
      "type": "message",
      "message": {
        "id": "message_1"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:01Z",
      "type": "account_event",
      "account_event": {
        "id": "account_event_1"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:02Z",
      "type": "message",
      "message": {
        "id": "message_2"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:03Z",
      "type": "account_event",
      "account_event": {
        "id": "account_event_2"
      }
    }
  ]
}


而查询:

GET example_index/_search
{
  "_source": "inner_hits", 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": 1
          }
        },
        {
          "nested": {
            "inner_hits": {
              "sort": [
                {
                  "conversation_events.occurred_at": {
                    "order": "desc"
                  }
                }
              ]
            },
            "path": "conversation_events",
            "query": {
              "match_all": {}
            }
          }
        }
      ]
    }
  }
}


回应:

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.0,
    "hits" : [
      {
        "_index" : "example_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.0,
        "_source" : { },
        "inner_hits" : {
          "conversation_events" : {
            "hits" : {
              "total" : {
                "value" : 4,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 3
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:03Z",
                    "account_event" : {
                      "id" : "account_event_2"
                    },
                    "type" : "account_event"
                  },
                  "sort" : [
                    1672574403000
                  ]
                },
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 2
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:02Z",
                    "type" : "message",
                    "message" : {
                      "id" : "message_2"
                    }
                  },
                  "sort" : [
                    1672574402000
                  ]
                },
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 1
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:01Z",
                    "account_event" : {
                      "id" : "account_event_1"
                    },
                    "type" : "account_event"
                  },
                  "sort" : [
                    1672574401000
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}


如果你想运行我的设置,这里是docker-compose文件:

version: "3"
services:
  elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2
    environment:
      - cluster.name=es-docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
      - bootstrap.system_call_filter=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - elastic

  kibana:
    image: docker.elastic.co/kibana/kibana-oss:7.10.2
    platform: linux/amd64
    container_name: kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elastic:9200
      - ELASTICSEARCH_URL=http://elastic:9200
    ports:
      - 5601:5601
    networks:
      - elastic
    depends_on:
      - elastic

volumes:
  data01:
    driver: local

networks:
  elastic:
    driver: bridge

vlurs2pr

vlurs2pr1#

很棒的复制品。谢谢!你得到了前3个点击,这是预期的,因为3是size参数的默认值。如果你需要更多,只需添加更高值的size参数。

GET example_index/_search
{
  "_source": "inner_hits", 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": 1
          }
        },
        {
          "nested": {
            "inner_hits": {
              "sort": [
                {
                  "conversation_events.occurred_at": {
                    "order": "desc"
                  }
                }
              ],
              "size": 10
            },
            "path": "conversation_events",
            "query": {
              "match_all": {}
            }
          }
        }
      ]
    }
  }
}

字符串

相关问题