java - 使用 Java API 的 BoolQuery 中的 NestedQuery

标签 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"
            }
          }
        }
      }
    }
  }
}

最佳答案

Elasticsearch 正在提示,因为您的 "user" 字段不是 nested 类型字段。您可以对“user.id”使用标准matchterm查询。

JSON 查询看起来像这样:

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

关于java - 使用 Java API 的 BoolQuery 中的 NestedQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52994979/

相关文章:

java - 如何删除日期(dd/mm/yyyy)中的年份(yyyy)

java - javax.net 的调试信息未写入 Domino 服务器控制台

elasticsearch - Elasticsearch “AND in query_string”与 “default_operator AND”

node.js - 如何在Node.js中添加多行无痛代码

logstash - 如何使用自定义 Logstash grok 模式?

java - 为什么 RouterFunctions.DefaultRouterFunction<T extends ServerResponse> 类中的谓词字段会获取 DefaultErrorWebExceptionHandler?

java - Elasticsearch Java API 术语聚合怪异

elasticsearch - 使用filebeat将结构化日志数据直接推送到elasticsearch

elasticsearch - Filebeat添加不需要的空白字段

java - Logstash stdout - 写入文件