Elasticsearch Search Fields - python-3.x

I am trying to search for a particular string in two fields. Right I am able to get the desired result. Now I want to rank the result in such a way that if value found in one key, it should be given more priority. I tried doing the following way but got error.
res = es.search(index="pdf_test", body={
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "72141",
}
}
]
}
},
"search_fields": {
"row_data": {
"weight": 10
},
"page_text": {
"weight": 1
}
}
})
I got the follwing error
elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', 'Unknown key for a START_OBJECT in [search_fields].')
I also tried the folllowing query as shown here
res = es.search(index="pdf_test", body={
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "72141",
"search_fields": {
"row_data": {
"weight": 10
},
"page_text": {
"weight": 1
}
}
}
}
]
}
}
})
elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[multi_match] unknown token [START_OBJECT] after [search_fields]')

You can rank the search result by using "^" in with their names. In your case it would be something like this.
res = es.search(index="pdf_test", body={
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "72141",
"fields": ["row_data^10", "page_text^1"]
}
}
]
}
}
})

Related

How do I create an “or” Condition filter using elasticsearch-dsl-py?

The query below is what I would like to construct using elasticsearch-dsl-py, but I do not know how to do it.
GET /my_index/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"or": {
"filters": [
{
"term": {
"status": "a"
},
"term": {
"x_status": "a"
},
}
]
}
}
}
}
}
}
I just want to execute a query like below in SQL format
select * from my_index where status = "a" or x_status="a"
I'm not sure which version of ES you're running, but just know that filtered has been replaced by bool a long time ago in version 5. So your query can be rewritten like this:
GET /my_index/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"status": "a"
}
},
{
"term": {
"x_status": "a"
}
}
]
}
}
}
Using elasticsearch-dsl-py, this translates to:
s = Search()
s = s.query('bool', should=[Q('term', status='a'), Q('term', x_status='a')])

ElasticSearch bool query for range of dates

I am new to ElasticSearch and looking for bool query to pass it to get the data from elasticsearch in spark scala code.
Here is my query:
Get all records for the eventName = "XXXXXX" and date between("1438367180542","1738367180542")
Could you please help me to write the elasticsearch query. Below is the one I tried but its giving error.
GET _search
{
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": "1438367180542",
"lte": "1738367180542"
}
}
}
],
"term": {
"eventName.keyword": "XXXXXXX"
}
}
}
}
Here is the error message:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[bool] query does not support [term]",
"line": 15,
"col": 19
}
],
"type": "parsing_exception",
"reason": "[bool] query does not support [term]",
"line": 15,
"col": 19
},
"status": 400
}
You're almost there! As you can see your range query is surrounded by curly braces, but your term query isn't and is out of the must array. Simply add those and move it to the must array and it will work. Even better use filter instead of must since you only have filters (i.e. you're not interested in scoring here)
GET _search
{
"query": {
"bool": {
"filter": [
{
"range": {
"date": {
"gte": "1438367180542",
"lte": "1738367180542"
}
}
},
{
"term": {
"eventName.keyword": "XXXXXXX"
}
}
]
}
}
}

Elasticsearch keyword + range query (V-6.2)

I am trying to write a query in Elasticsearch to make it work with Range filter and query keyword input from user.
The query that I end up writing is:
"size": val, //default 10,
"from": 0, //default 0,
"query": {
"bool": {
"must": {
"query_string": {
"query": search_query //Val coming from user input
},
"filter": {
"range": {
"lastmodifieddate": {
"gte": '2016-12-09T00:00:00',
"lte": '2016-12-20T00:00:00'
}
}
}
}
}
}
The above query is not working.
Also I am looking for matching 1 of the key value pair from my elasticsearch.
_source:
lastmodifieddate: "2016-12-07T18:34:48.000+0000",
..
..
fileType: "PDF"
...
Can someone throw some light on how to make it work and also a query parameter with all records must match fileType = PDF
TIA
You must put "filter" outside "must". Both of them are in different context. See this documentation https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
{
"query": {
"bool": {
"must": {
"query_string": {
"query": "user input"
}
},
"filter": {
"range": {
"lastmodifieddate": {
"gte": "2014-01-09T00:00:00",
"lte": "2014-12-20T00:00:00"
}
}
}
}
}
}

ElasticSearch : How to combine nested 'AND' Not Equal

I want build query for search matching with nested and not equal.
This is my elasticSearch query:
{
"from":0,"size":1000,
"query":{
"nested" : {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
},
"filter":{
"not":{
"filter":{
"term":{
"_id":"01921asda01201"
}
}
}
}
}
}
But I got error, am I write something wrong ? thanks
You can use bool Filter too with must and must_not clause.
{
"from": 0,
"size": 1000,
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "data",
"query": {
"match": {
"data.city": "california"
}
}
}
}
],
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
You need to use filtered query
GET _search
{
"query": {
"filtered": {
"query": {
"nested": {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
}
},
"filter": {
"bool": {
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
}
}
You should use a bool query for this, and put your two clauses in the must and must_not sections respectively.
If you don't care about scoring on the data.city field (from your example it's not clear), you might want to use the filter portion instead of the must portion.
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "data",
            "query": {
              "match": {
                "data.city": "california"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "_id": "01921asda01201"
          }
        }
      ]
    }
  }
}

ElasticSearch: Show partial match for multi search even if one field does not match

I am currently trying to do a multi search query on first name, last name, and date of birth. I want the results to show a complete match for first, last, and dob but also show results if the first name and last name match but a different date of birth exists then what was queried on.
As of right now my code only returns a result if all three fields have exact matches
GET /account/data/_search
{
"query": {
"match": {
"first": {
"query": "Chris"
}
}
},
"query": {
"match": {
"last": {
"query": "Johnson"
}
}
},
"query": {
"match": {
"dob": {
"query": "10-10-1990"
}
}
}
}
This can be solved with simple bool query
{
"query": {
"bool": {
"must": [
{
"match": {
"first": "TEXT"
}
},
{
"match": {
"last": "TEXT"
}
}
],
"should": [
{
"match": {
"dob": "TEXT"
}
}
]
}
}
}

Resources