# Python: How to input a string and use it as query match elasticsearch

**URL:** <https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707>\
**Category:** Elasticsearch\
**Created:** [June 24, 2017, 9:28am UTC](https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707 "2017-06-24T09:28:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riccardo\_Spampinato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riccardo_spampinato/32/18534_2.png) [@Riccardo\_Spampinato](https://discuss.elastic.co/u/Riccardo_Spampinato)\
**Post date:** [June 24, 2017, 9:28am UTC](https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707/1 "2017-06-24T09:28:05Z")

</div>

Hi everyone,  
I'm trying to code a simple `python` script which should ask for a string to be inserted by shell and use it as "match string" in a `match` query. I post here the script so the problem can be better understandable.

```
client = Elasticsearch()

query_string = raw_input("Enter your query string: ")
print(query_string)

s = Search(using=client, index="enron_test"
    .query("match", message_body=query_string))

response = s.execute()

```

Actually, I should get all the `json` documents (each represents an email) which contain the string "query\_string" in the `message_body` field.

The problem is that when I run the script, I get this error: `AttributeError: 'str' object has no attribute 'query'`.  
I'm a newbie in `Elasticsearch`, where am I wrong?

---

<div class="post-metadata">

**Author:** ![timost](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@timost](https://discuss.elastic.co/u/timost)\
**Post date:** [June 25, 2017, 5:32am UTC](https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707/2 "2017-06-25T05:32:50Z")

</div>

I think you have a typo in your code and that one of the closing parenthesis is misplaced. Try this instead:

```auto
s = Search(using=client, index="enron_test")
    .query("match", message_body=query_string)

```

---

<div class="post-metadata">

**Author:** ![Riccardo\_Spampinato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riccardo_spampinato/32/18534_2.png) [@Riccardo\_Spampinato](https://discuss.elastic.co/u/Riccardo_Spampinato)\
**Post date:** [June 25, 2017, 9:58am UTC](https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707/3 "2017-06-25T09:58:47Z")

</div>

@timost It works!!!

EDIT:  
I was trying to print the result of the query and I figured out that I cannot retrieve `hit.from` of the `json` document.  
It tells me `SyntaxError: invalid syntax`.

Here is the code:

```
for hit in response:
    print(hit.from)
    print(hit.to)
    print(hit.message_body)
    print(line + "\n")

```

The `from` field is highlighted in grey.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 23, 2017, 9:58am UTC](https://discuss.elastic.co/t/python-how-to-input-a-string-and-use-it-as-query-match-elasticsearch/90707/4 "2017-07-23T09:58:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
