0

Suppose we have the query:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "brown fox",
      "type":       "best_fields",
      "fields":     [ "subject", "message" ],
      "tie_breaker": 0.3
    }
  }
}

which would be executed as:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "match": { "message": "brown fox" }}
      ],
      "tie_breaker": 0.3
    }
  }
}

suppose that the field "message" can be searchable if the text is only of length 5.

How can I perform the same logic that would be executed like:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "match": { "message": "brown" }}
      ],
      "tie_breaker": 0.3
    }
  }
}

The example is taken from here

1 Answers1

0

So, if you are trying to just do an exact match on the 'message' (since brown is 5 letters, you can read the following: https://www.elastic.co/guide/en/elasticsearch/guide/master/_finding_exact_values.html#_finding_exact_values

If you would like to do a match against whatever analyzer you'd like while checking the length, you can use an analyzer to do the string length and another to perform the match.

`

PUT ...index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "character_analyzer": {
          "type": "custom",
          "tokenizer": "character_tokenizer"
        }
      },
      "tokenizer": {
        "character_tokenizer": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 1
        }
      }
    }
  }, 
  "mappings": {
    "<object>": {
      "properties": {
        "message": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            },
            "length": { 
              "type": "token_count",
              "analyzer": "character_analyzer"
            }
          }
        }
      }
    }
  }
}

`

Then you should be able to do a match with an and condition:

`

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "must" : [
           { "term" : { "message.length" : 5 }},
           { "term" : { "message.keyword" : "brown" }}
        ]}
      ],
      "tie_breaker": 0.3
    }
  }
}

`

Finally, you can also use scripting instead of adding another analyzer, as mentioned in this post: Kibana querying for string length

`

{
    "query": {
        "filtered": {
            "filter": {
                "script": {
                    "script": "doc['key'].getValue().length() > 5"
                }
            }
        }
    }
}

`

user2740775
  • 77
  • 1
  • 6