Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific sized result from fields?

AS I know that We can control the size of the highlighted fragment. But I want to control the size of the specific field result.

ex) I can control size highlighted by using fragment_size, number_of_fragments.

highlight: {
    "CONTEXT": [
        "I am leaving today.I want to be a part of it New York, New York These <b>vagabond</b> shoes They are"
    ]
}

But I want to get result as like this;

fields: {
    "CONTEXT": [
        "I am leaving today.I want to be a part of it New York, New York These vagabond shoes They are longing to stray Right through the very heart of it New York, New York ..."
    ],
    "TITLE": [
         "new york new york"
    ]
},

Because, Sometimes This context field is too long to display result Web Page. So, I want to control size CONTEXT field result.

like image 579
guradona Avatar asked Feb 23 '26 00:02

guradona


1 Answers

You can do something like the following, but I'd suggest if you can to do this on the client side.

{
  "settings": {
    "analysis": {
      "filter": {
        "truncate_filter": {
          "type": "truncate",
          "length": 20
        }
      },
      "analyzer": {
        "my_truncate_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "truncate_filter"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "test": {
      "properties": {
        "context": {
          "type": "string",
          "fields": {
            "truncated": {
              "type": "string",
              "analyzer": "my_truncate_analyzer"
            }
          }
        }
      }
    }
  }
}

And in your query you'd need to add a fielddata_fields to see the truncated field:

{
  "query": {
    ...
  }, 
  "fielddata_fields": ["context.truncated"]
}
like image 67
Andrei Stefan Avatar answered Feb 26 '26 13:02

Andrei Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!