Skip to content Skip to sidebar Skip to footer

Elasticsearch Multiple-query/search With Python

I'm new to Python and Elasticsearch and I have created an index with some data in Elasticsearch and I want to perform a query on them with Python based on some filters that are rec

Solution 1:

Adding a working example with index data, search query, and search result

According to your comments mentioned above, if the content field does not contain keyword, and if the category field contains category, then search query will execute for category field. This can be achieved by using minimum_should_match

Index Data:

{
    "content": "keyword a",
    "title": "b",
    "lead": "c",
    "category": "d"
}
{
    "content": "a",
    "title": "b",
    "lead": "c",
    "category": "category"
}
{
    "content": "keyword a",
    "title": "b",
    "lead": "c",
    "category": "category"
}

Search Query:

{"query":{"bool":{"should":[{"multi_match":{"query":"keyword","fields":["content","title","lead"]}},{"multi_match":{"query":"category","fields":["category"]}}],"minimum_should_match":1}}}

Search Result:

"hits": [
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9666445,
        "_source": {
          "content": "keyword a",
          "title": "b",
          "lead": "c",
          "category": "category"
        }
      },
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.60996956,
        "_source": {
          "content": "keyword a",
          "title": "b",
          "lead": "c",
          "category": "d"
        }
      },
      {
        "_index": "stof_64081587",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.35667494,
        "_source": {
          "content": "a",
          "title": "b",
          "lead": "c",
          "category": "category"
        }
      }
    ]

Post a Comment for "Elasticsearch Multiple-query/search With Python"