1

I am a beginner in elasticsarch and I wanted this query below to work with the two filters, having two range of different fields, but only the first range is working.

This filter is working normally:

"range" : {"pgrk" : { "gte" : 1, "lte" : 10} }

Could someone tell me why this second filter below doesn't work?

"should" : { "range" : {"url_length" : { "lte" : 100 } }

--------------------------Follow my query below with the two filters--------------------------

 {

    "from" : 0, "size" : 10,

    "sort" : [
            { "pgrk" : {"order" : "desc"} },

             { "url_length" : {"order" : "asc"} }
        ],

        "query": {

    "bool": {
    "must": {

    "multi_match" : {
    "query": "netflix",

    "type": "cross_fields",
    "fields": [ "titulo", "descricao", "url" ],
    "operator": "and"
            }
         },
         "filter": {
         "range" : {"pgrk" : { "gte" : 1, "lte" : 10}    }
          },


    "should" : {
            "range" : {"url_length" : { "lte" : 100 } }
            }

            }
    }


          }
0

1 Answer 1

1

Not sure, what is your requirement as index mapping and sample documents are not provided but I created my own mapping and sample documents to show you how to create multiple range queries in filter context.

Please comment, so that I can modify if its results are not according to your requirements.

Index Def

{
    "mappings": {
        "properties": {
            "title": {
                "type": "text"
            },
            "url": {
                "type": "keyword"
            },
            "pgrk": {
                "type": "integer"
            },
            "url_length": {
                "type": "integer"
            }
        }
    }
}

Index sample docs

{
    "title": "netflix",
    "url" : "www.netflix.com", --> this shouldn't match as `pgrk > 10`
    "pgrk": 12,
    "url_length" : 50
}

{
    "title": "Netflix",  --> this should match both filetrs
    "url" : "www.netflix.com",
    "pgrk": 8,
    "url_length" : 50
}

{
    "title": "Netflix", --> this should match both filetrs
    "url" : "www.netflix",
    "pgrk": 5,
    "url_length" : 50
}

{ "title": "netflix", "url" : "www.netflix", "pgrk": 5, "url_length" : 80. --> note pgrk has same 5 as prev and url_length is diff }

Search query

{
    "from": 0,
    "size": 10,
    "sort": [
        {
            "pgrk": {
                "order": "desc"
            }
        },
        {
            "url_length": {
                "order": "asc"
            }
        }
    ],
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "netflix",
                    "type": "cross_fields",
                    "fields": [
                        "title",
                        "url"
                    ],
                    "operator": "and"
                }
            },
            "filter": [ --> note filter array to have multiple range queries in filter context
                {
                    "range": {
                        "pgrk": {
                            "gte": 1,
                            "lte" : 10
                        }
                    }
                },
                {
                    "range": {
                        "url_length": {
                            "lte": 100
                        }
                    }
                }
            ]
        }
    }
}

And search result which brings only three docs (even 2 has same pgrk value)

 "hits": [
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix.com",
                    "pgrk": 8,
                    "url_length": 50
                },
                "sort": [
                    8,
                    50
                ]
            },
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "3",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix",
                    "pgrk": 5,
                    "url_length": 50
                },
                "sort": [
                    5,
                    50
                ]
            },
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "4",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix",
                    "pgrk": 5,
                    "url_length": 80
                },
                "sort": [
                    5,
                    80
                ]
            }
        ]
Sign up to request clarification or add additional context in comments.

7 Comments

If you create docs with the field (url_length) with different values, the query only works through the field filter (pgrk). The filter (url_length) does not work
@Jean, I tried with different value of url_length and it works fine for that, updating the answer
Hello @Opster Let's say that in my mappings I have a name field (URL: "type": "keyword") that stores the urls of the indexed sites, there is some function in elasticsearch in which when indexing a DOC the elasticsearch itself counts the size of the Stored URL, without having to make this count of the URL size via PHP. Because I want to sort the results returned in the query by the size of the URL, from the smallest to the largest value. Thanks
@Jean, out of the box, elasticsearch doesn't provide such functionality, I am assuming size means MB here, you can do some hacks on elastic but that would be complex and as you are new to elastic wouldn't recommend that for you as it can lead to some unexpected results and poor performance which would be difficult for you to troubleshoot. you are already doing the right thing to calculate this size in you application and storing it in different field in Elastic :)
Hello @Opster I unselected the answer because I thought I couldn't ask any further questions if it was marked as done. I already scored again. So the best way to know the size of the URL would be through a script like in PHP?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.