Search code examples
elasticsearchelasticsearch-aggregation

Elastic search : multiple criterias on text search


I've a question to setting my index in elastic Search.

My index has 3 fields: desig, desigSec and idRef. In my UI application, i've a single input to search in theses 3 fields.

So i've defined this settings :

    {
      "settings": {
        "index.max_ngram_diff": 17,
        "analysis": {
          "tokenizer": {
            "ngram_tokenizer": {
              "type": "ngram",
              "min_gram": 3,
              "max_gram": 20,
              "token_chars": [
                "letter",
                "digit"
              ]
            }
          },
          "analyzer": {
            "ngram_analyzer": {
              "type": "custom",
              "tokenizer": "ngram_tokenizer",
              "filter": [
                "lowercase"
              ]
            },
            "standard_analyzer": {
              "type": "custom",
              "tokenizer": "standard",
              "filter": [
                "lowercase"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "desig": {
            "type": "text",
            "analyzer": "ngram_analyzer",
            "search_analyzer": "standard_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "desigSec": {
            "type": "text",
            "analyzer": "ngram_analyzer",
            "search_analyzer": "standard_analyzer"
          },
          "idRef": {
            "type": "text",
            "analyzer": "ngram_analyzer",
            "search_analyzer": "standard_analyzer"
          }
        }
      }
    }

I've an article

    desig='Medicine'
    desig2='B0715246'
    idRef='Cert-038'

but 0 result with this query :

    {
        "query": "B07 Cert-038",
        "type": "best_fields",
        "fields": [
            "desig",
            "desigSec",
            "idRef"
        ],
        "operator": "and"
    }

i use 'and' operator to have more precision in my search, if i use 'or' it aggregates all the results for B07 or Cert-038 and i don't want that.

i think it's not the good approach, please help me and thanks for your advices because i don't have any more ideas... (juste maybe aggregate the 3 fields in single field when i create my index ?)


Solution

  • i found my answer :

    {
      "combined_fields": {
          "query": "B07 Cert-038",
          "fields": ["desig", "desigSec", "idRef"],
          "operator": "and"
      }
    }