Anonymous
Как выполнить фасетный поиск по нескольким вложенным полям в клиенте Elasticsearch Java API?
Сообщение
Anonymous » 17 ноя 2024, 14:03
РЕШЕНО
Код: Выделить всё
Query characteristicQuery1 = NestedQuery.of(n -> n
.path("characteristics")
.query(q -> q
.bool(b -> b
.must(m -> m
.term(t -> t
.field("characteristics.characteristic_id")
.value("1")
.field("characteristics.text_value")
.value("blue"))))))._toQuery();
Query characteristicQuery2 = NestedQuery.of(n -> n
.path("characteristics")
.query(q -> q
.bool(b -> b
.must(m -> m
.term(t -> t
.field("characteristics.characteristic_id")
.value("2")
)
).must(m -> m
.range(r -> r
.field("characteristics.numeric_value")
.gte(JsonData.of(1))
.lte(JsonData.of(5)))))))._toQuery();
Query termQuery = TermQuery.of(t -> t
.field("category_id")
.value("1"))._toQuery();
Query priceQuery = RangeQuery.of(r -> r
.field("price")
.gte(JsonData.of(1.0))
.lte(JsonData.of(500.0)))._toQuery();
List list = new ArrayList();
list.add(characteristicQuery1);
list.add(characteristicQuery2);
list.add(termQuery);
list.add(priceQuery);
BoolQuery boolQuery = BoolQuery.of(b -> b
.filter(list)
.should(s -> s
.match(m -> m
.field("title")
.query("te"))));
SearchResponse
response = esClient.search(s -> s
.index("product")
.query(q -> q.bool(boolQuery)),
ProductDocument.class);
Это мои примеры документов.
Код: Выделить всё
"id" : "1",
"title" : "test",
"description" : "test",
"price" : 100.0,
"category_id" : "1",
"characteristics" : [
{
"characteristic_id" : "1",
"text_value" : "red"
},
{
"characteristic_id" : "2",
"numeric_value" : 15
},
{
"characteristic_id" : "3",
"numeric_value" : 20
}
]
"id" : "2",
"title" : "test",
"description" : "test",
"price" : 200.0,
"category_id" : "1",
"characteristics" : [
{
"characteristic_id" : "1",
"text_value" : "blue"
},
{
"characteristic_id" : "2",
"numeric_value" : 10
},
{
"characteristic_id" : "3",
"numeric_value" : 5
}
]
И запрос к моему индексу должен быть таким. Как я могу написать это, используя новый клиент Java Api для Elasticsearch?
Код: Выделить всё
GET product/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "characteristics",
"query": {
"bool": {
"must": [
{
"term": {
"characteristics.characteristic_id": 1
}
},
{
"term": {
"characteristics.text_value": "blue"
}
}
]
}
}
}
},
{
"nested": {
"path": "characteristics",
"query": {
"bool": {
"must": [
{
"term": {
"characteristics.characteristic_id": 3
}
},
{
"range": {
"characteristics.numeric_value": {
"gte": 1,
"lte": 7
}
}
}
]
}
}
}
},
{
"term": {
"category_id": 1
}
},
{
"range": {
"price": {
"gt": 10.0,
"lt": 500.0
}
}
}
],
"should": [
{
"match": {
"title": "te"
}
}
]
}
}
}
В официальной документации очень мало информации. Стоит ли вообще использовать этот API, если к нему нет нормальной документации, и писать все запросы вручную?
Подробнее здесь:
https://stackoverflow.com/questions/791 ... va-api-cli
1731841382
Anonymous
[b]РЕШЕНО[/b] [code] Query characteristicQuery1 = NestedQuery.of(n -> n .path("characteristics") .query(q -> q .bool(b -> b .must(m -> m .term(t -> t .field("characteristics.characteristic_id") .value("1") .field("characteristics.text_value") .value("blue"))))))._toQuery(); Query characteristicQuery2 = NestedQuery.of(n -> n .path("characteristics") .query(q -> q .bool(b -> b .must(m -> m .term(t -> t .field("characteristics.characteristic_id") .value("2") ) ).must(m -> m .range(r -> r .field("characteristics.numeric_value") .gte(JsonData.of(1)) .lte(JsonData.of(5)))))))._toQuery(); Query termQuery = TermQuery.of(t -> t .field("category_id") .value("1"))._toQuery(); Query priceQuery = RangeQuery.of(r -> r .field("price") .gte(JsonData.of(1.0)) .lte(JsonData.of(500.0)))._toQuery(); List list = new ArrayList(); list.add(characteristicQuery1); list.add(characteristicQuery2); list.add(termQuery); list.add(priceQuery); BoolQuery boolQuery = BoolQuery.of(b -> b .filter(list) .should(s -> s .match(m -> m .field("title") .query("te")))); SearchResponse response = esClient.search(s -> s .index("product") .query(q -> q.bool(boolQuery)), ProductDocument.class); [/code] Это мои примеры документов. [code]"id" : "1", "title" : "test", "description" : "test", "price" : 100.0, "category_id" : "1", "characteristics" : [ { "characteristic_id" : "1", "text_value" : "red" }, { "characteristic_id" : "2", "numeric_value" : 15 }, { "characteristic_id" : "3", "numeric_value" : 20 } ] "id" : "2", "title" : "test", "description" : "test", "price" : 200.0, "category_id" : "1", "characteristics" : [ { "characteristic_id" : "1", "text_value" : "blue" }, { "characteristic_id" : "2", "numeric_value" : 10 }, { "characteristic_id" : "3", "numeric_value" : 5 } ] [/code] И запрос к моему индексу должен быть таким. Как я могу написать это, используя новый клиент Java Api для Elasticsearch? [code]GET product/_search { "query": { "bool": { "must": [ { "nested": { "path": "characteristics", "query": { "bool": { "must": [ { "term": { "characteristics.characteristic_id": 1 } }, { "term": { "characteristics.text_value": "blue" } } ] } } } }, { "nested": { "path": "characteristics", "query": { "bool": { "must": [ { "term": { "characteristics.characteristic_id": 3 } }, { "range": { "characteristics.numeric_value": { "gte": 1, "lte": 7 } } } ] } } } }, { "term": { "category_id": 1 } }, { "range": { "price": { "gt": 10.0, "lt": 500.0 } } } ], "should": [ { "match": { "title": "te" } } ] } } } [/code] В официальной документации очень мало информации. Стоит ли вообще использовать этот API, если к нему нет нормальной документации, и писать все запросы вручную? Подробнее здесь: [url]https://stackoverflow.com/questions/79196103/how-to-make-facet-search-by-multiple-nested-fields-in-elasticsearch-java-api-cli[/url]