使用Java API在BoolQuery中进行NestedQuery

时间:2018-10-25 17:29:09

标签: java elasticsearch elastic-stack

我正试图了解EL中的查询的工作原理,说实话还有很多问题。

这里的文档具有属性:

{"statusError":null,
"fileHash":"da8620bad21685c5e385fb1b43a7e744",
"project":{"id":7687},
"error":null,
"ocrFile64":"JVBERi0xL...."
"isInElastic":false,
"originalName":"test.pdf",
"lastUpdated":"2018-10-18T12:47:59Z",
"dateCreated":"2018-10-18T12:40:19Z",
"ocrAvailable":true,
"attachment":{"date":"2018-07-05T07:20:06Z",
"content_type":"application/pdf",
"language":"en","title":"Untitled",
"content":"blah blah blahblahblahblahblah"
"company":{"id":1},
"id":25850,
"tag":[{"id":3},{"id":2}],
"contentType":"application/pdf",
"imageHash":"",
"label":null,
"size":47680,
"user":{"id":7563},
"md5":[100,97,56,54,50,48,98,97,100,50,49,54,56,53,99,53,101,51,56,53,102,98,49,98,52,51,97,55,101,55,52,52],
"status":{"name":"CLASSIFIED"}}

EL的Ingest Module插件已安装,用于上传文件内容。确实,管道是ocrFile64,文件的内容在内容属性内部。

我想做的事情很简单,我想做一个查询:给我所有原始名称包含“ test”且user.id等于1且内容包含“ blah”的文档。

到目前为止,我已经写了这个:

client = new RestHighLevelClient(builder)
SearchRequest searchRequest = new SearchRequest("testEL")
searchRequest.types("test")
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS))
BoolQueryBuilder boolQuery = new BoolQueryBuilder();

boolQuery.filter(new MatchPhrasePrefixQueryBuilder("originalName", "test"))

boolQuery.filter(new NestedQueryBuilder("user", new MatchQueryBuilder("user.id", "1"), ScoreMode.None))

boolQuery.filter(new MatchPhrasePrefixQueryBuilder("content", "blah"))

searchSourceBuilder.query(boolQuery)
searchRequest.source(searchSourceBuilder)
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

如果我仅查询orignalName,则它起作用。如果不再添加内容,则如果添加嵌套查询,则会导致错误:

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

谢谢

在这里映射:

{
  "mapping": {
    "test": {
      "properties": {
        "attachment": {
          "properties": {
            "content": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "content_length": {
              "type": "long"
            },
            "content_type": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "date": {
              "type": "date"
            },
            "language": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "title": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "company": {
          "properties": {
            "id": {
              "type": "long"
            }
          }
        },
        "contentType": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "dateCreated": {
          "type": "date"
        },
        "fileHash": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "id": {
          "type": "long"
        },
        "imageHash": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "isClassified": {
          "type": "boolean"
        },
        "isInElastic": {
          "type": "boolean"
        },
        "label": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "lastUpdated": {
          "type": "date"
        },
        "md5": {
          "type": "long"
        },
        "ocrAvailable": {
          "type": "boolean"
        },
        "ocrFile64": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "originalName": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "project": {
          "properties": {
            "id": {
              "type": "long"
            }
          }
        },
        "size": {
          "type": "long"
        },
        "status": {
          "properties": {
            "enumType": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "storageName": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "tag": {
          "properties": {
            "id": {
              "type": "long"
            }
          }
        },
        "user": {
          "properties": {
            "id": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

Elasticsearch抱怨是因为您的"user"字段不是nested类型的字段。您可以在match上使用标准term"user.id"查询。

JSON查询如下所示:

POST <your_index>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "originalName": "test"
          }
        },
        {
          "match": {
            "user.id": 1
          }
        },
        {
          "match": {
            "content": "blah"
          }
        }
      ]
    }
  }
}