Home > Software design >  I need help for a query elasticsearch
I need help for a query elasticsearch

Time:05-15

I need help for a query.

This is my query and my sample :

GET /product/_search
{
  "query": {
    "bool" : {
      "must" : {
        "multi_match" : {
          "query":    "Torsades", 
          "fields": [ "ean^10", "name^4", "brand" ] 
        }
      }
    }
  }
}

[
  {
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 13.78764,
    "_source" : {
      "country" : 1,
      "ean" : "3250391967858",
      "name" : "Torsades Semi-complètes BIO - 500G",
      "brand" : "Fiorini"
    }
  },
  {
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "74",
    "_score" : 13.78764,
    "_source" : {
      "country" : null,
      "ean" : "3564700009826",
      "name" : "Pâtes Torsades - Turini - 500 g",
      "brand" : "Turini"
    }
  },
  {
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "78",
    "_score" : 11.964245,
    "_source" : {
      "country" : null,
      "ean" : "3250391967858",
      "name" : "Torsades Semi-complètes BIO - 500G - ITM BENCHMARK",
      "brand" : "Fiorini"
    }
  }
]

I want a condition specific and I can't find the solution :

I want :

ALL products for country=1 AND (ALL products for country=null MINUS product.ean IN country=1)

In my sample, I want have 2 hits :

THIS is deleted because EAN in country=1 :

{
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "78",
    "_score" : 11.964245,
    "_source" : {
      "country" : null,
      "ean" : "3250391967858",
      "name" : "Torsades Semi-complètes BIO - 500G - ITM BENCHMARK",
      "brand" : "Fiorini"
    }
}

Someone have a solution ?

EDIT : I want this result :

[
  {
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 13.78764,
    "_source" : {
      "country" : 1,
      "ean" : "3250391967858",
      "name" : "Torsades Semi-complètes BIO - 500G",
      "brand" : "Fiorini"
    }
  },
  {
    "_index" : "product_2022-05-13-194440",
    "_type" : "_doc",
    "_id" : "74",
    "_score" : 13.78764,
    "_source" : {
      "country" : null,
      "ean" : "3564700009826",
      "name" : "Pâtes Torsades - Turini - 500 g",
      "brand" : "Turini"
    }
  }
]

CodePudding user response:

You tried to use Field Collapsing?

GET test/_search
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "Torsades",
          "fields": [
            "ean^10",
            "name^4",
            "brand"
          ]
        }
      }
    }
  },
  "collapse": {
    "field": "ean.keyword"
  }
}

Response:

  "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5611319,
        "_source" : {
          "country" : 1,
          "ean" : "3250391967858",
          "name" : "Torsades Semi-complètes BIO - 500G",
          "brand" : "Fiorini"
        },
        "fields" : {
          "ean.keyword" : [
            "3250391967858"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.5611319,
        "_source" : {
          "country" : null,
          "ean" : "3564700009826",
          "name" : "Pâtes Torsades - Turini - 500 g",
          "brand" : "Turini"
        },
        "fields" : {
          "ean.keyword" : [
            "3564700009826"
          ]
        }
      }
    ]
  • Related