在弹性搜索中,如何匹配对象数组中的多个对象?

时间:2019-03-04 11:15:42

标签: elasticsearch

我在这里遵循示例:

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

以下查询:

# state convolution
state_input = Input(shape=INPUT_SHAPE, name='state_input', batch_shape=(1, 210, 160, 3))
state_Conv2D_1 = Conv2D(8, kernel_size=(8, 8), strides=(4, 4), activation='relu', name='state_Conv2D_1')(state_input)
state_MaxPooling2D_1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='state_MaxPooling2D_1')(state_Conv2D_1)
state_outputs = Flatten(name='state_Flatten')(state_MaxPooling2D_1)
state_convolution_model = Model(state_input, state_outputs, name='state_convolution_model')
state_convolution_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['acc'])

state_convolution_model_input = Input(shape=INPUT_SHAPE, name='state_convolution_model_input', batch_shape=(1, 210, 160, 3))
state_convolution = state_convolution_model(state_convolution_model_input)

# classification output
classficication_Input = Input(shape=(1, LSTM_OUTPUT_DIM), batch_shape=(1, LSTM_OUTPUT_DIM), name='classification_prediction_Input')
classficication_Dense_1 = Dense(32, activation='relu', name='classification_prediction_Dense_1')(classficication_Input)
classficication_output_raw = Dense(ACTIONS, activation='sigmoid', name='classification_output_raw')(classficication_Dense_1)
classficication_output = Reshape((ACTIONS,), name='classification_output')(classficication_output_raw)
classficication_model = Model(classficication_Input, classficication_output, name='classificationPrediction_model')
classficication_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

classficicationPrediction = classficication_model(state_convolution)

i = keras.layers.concatenate([state_outputs, classficication_output], name='concatenate')
d = Dense(32, activation='relu')(i)
o = Dense(1, activation='sigmoid')(d)
model = Model(state_input, o)                  # <-- graph error is here
plot_model(model, to_file='model.png', show_shapes=True)

符合预期的匹配一条记录。假设我只想返回同时具有用户“ John Smith”和“ Alice White”的文档。

我尝试过:

GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "White" }}
          ]
        }
      }
    }
  }
}

但是这将返回零结果。如何获得同时带有“爱丽丝·怀特”和“约翰·史密斯”的文档(应该与原始结果中返回的文档相同)?

1 个答案:

答案 0 :(得分:2)

您应该使用bool query组合多个子句。使用您的语法,您正在寻找一个在同一字段中同时包含Alice和John作为值的文档。确实可以尝试:

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "user",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "user.first": "Alice"
                    }
                  },
                  {
                    "match": {
                      "user.last": "White"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "user",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "user.first": "John"
                    }
                  },
                  {
                    "match": {
                      "user.last": "Smith"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}