0

I'm trying to query some indexed data with an array of strings as search input.

The indexed data looks like this:

{
  "pubMedID": "21528671",
  "title": "Basic fibroblast [...] melanoma cells.",
  "abstract": "Human malignant [...] cell growth."
}

I would like to search within the 'title' and 'abstract' fields for multiple strings. For example:

queryString=['melanoma', 'dysplastic nevus syndrome']

I already tried with the following code:

queryString=['melanoma', 'dysplastic nevus syndrome']

payload={
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": queryString,
            "fields": [
              "title",
              "abstract"
            ]
          }
        }
      ]
    }
  }
}


payload_json = (json.dumps(payload))
res = esclient.search(index='medicine',body=payload_json)

But I get the following error when running this:

RequestError: RequestError(400, 'parsing_exception', '[query_string] query does not support [query]')

The query does work fine if I just put in a simple string value. Can someone tell me how I should do this kind of queries where you give as an input an array? Thank you in advance!

6
  • depends what you're after and how your index is defined. If you want any of the space separated strings in any position, then just concatenate the strings with a space separation. If you want a result that contains ALL of the string in any position, then you need to build a more complex query and put a should clause for each string in your query array. In either case, the index/query needs to be correctly defined. Commented Mar 23, 2019 at 15:21
  • Thanks for your answer @bryan60 ! I'm looking for kind of an 'or' between all strings in the queryString. I don't want to concatenate them and threat them as one big search string. Commented Mar 23, 2019 at 15:23
  • why not if that's what you just stated you're after? that's how elastic full text search works. I'm not clear on the reason for treating them all as separate queries. Commented Mar 23, 2019 at 15:24
  • I will give you a more concrete example. I would like to search and check if 'melanoma' or 'dysplastic nevus syndrome' appears in the abstract or title, but not if 'melanoma dysplastic' appears in the abstract, because that would be wrong. Commented Mar 23, 2019 at 15:27
  • so that's the second use case i outlined, you need multiple clauses and build your query in a dynamic fashion. Commented Mar 23, 2019 at 15:29

1 Answer 1

1

EDIT:

I was a bit unfamiliar with the query_string query, but it turns out you can do something like this with it too:

qs = ''
for q in queryStrings:
  if qs:
    qs += ' OR '
  qs += q

payload={
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": qs,
            "fields": [
              "title",
              "abstract"
            ]
          }
        }
      ]
    }
  }
}

the result will be a query similar to the multiple clause one's outlined below.

docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

ORIGINAL:

this can be achieved with multiple clauses like so:

queryString=['melanoma', 'dysplastic nevus syndrome']

payload={
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": queryString[0],
            "fields": [
              "title",
              "abstract"
            ]
          }
        },
        {
          "query_string": {
            "query": queryString[1],
            "fields": [
              "title",
              "abstract"
            ]
          }
        }
      ]
    }
  }
}

If you have a variable number of queries, then you just need to dynamically build your "should" clauses like:

shoulds = []
for q in queryStrings:
   shoulds.append({
      "query_string": {
        "query": q,
        "fields": [
          "title",
          "abstract"
        ]
      }
    })

payload={
  "query": {
    "bool": {
      "should": shoulds
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Just remember that if you’re using the query_string string, you could get funky results if the user puts certain key words in the actual query. Consult the docs for how to handle these cases.

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.