1
votes

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 } }
            }

            }
    }


          }
2

2 Answers

1
votes

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
                ]
            }
        ]
0
votes

@Opster

How are you?

because when I use the (From / Size) function in the query, it doesn't return the doc that I hope to return. For example, I have 5 docs in elasticsearch, all docs in the sequence, id 1, id 2, id 3, id 4, id 5.

The doc ID 5 has the word (car) and In the query I say to get the word (car) from ID 4 ("from": 4, "size": 1,) it does not return the ID 5 it has the word (car), but in the query I put information that exists in doc 5, so I should bring doc 5, but it doesn't and says it didn't find a result. If I put the query from ID 0 ("from": 0, "size": 1,) it finds ID 5. It seems that elasticsearch makes confusion with ID's. Can you tell me what happens to ID's?

The query below from ID 4 does not return ID 5, but if I put the query from ID 0, then it returns ID 5

{
"from": 4, "size": 1,
"query": {
"multi_match": {
"query": "car",

   "type": "cross_fields",
   "fields": ["title", "description", "url"],
   "operator": "and"
}
}
}