# Lucene query for AND operator is not working

**URL:** <https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333>\
**Category:** Elasticsearch\
**Created:** [November 11, 2019, 10:54am UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333 "2019-11-11T10:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![debasish283](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/debasish283/32/46133_2.png) [@debasish283](https://discuss.elastic.co/u/debasish283)\
**Post date:** [November 11, 2019, 10:54am UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333/1 "2019-11-11T10:54:26Z")

</div>

We are trying to use lucene search based on two fields below is the query we tried but we are not getting appropriate results.

```
GET test/message/_search
{
  "size": 180,
  "_source": [
    "host_name",
    "value",
    "item_name"
  ],
  "query": {
    "query_string": {
      "query": "host_name: debasish AND item_name: Agent ping"
    }
  }
}

```

The result we are getting as empty, however if we are using OR operator we are getting the results accurately. but we need the documents to match hostname\_debasish and item\_name: Agent Ping, I know we can write using bool -\> must -\> match conditions but we have to implement this in lucene based search. Please share your thoughts

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 11, 2019, 12:54pm UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333/2 "2019-11-11T12:54:41Z")

</div>

How have your fields been mapped? Is `item_name` a `keyword` field? If so, try surrounding your query term with parentheses:

```auto
host_name: debasish AND item_name: (Agent ping)

```

---

<div class="post-metadata">

**Author:** ![debasish283](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/debasish283/32/46133_2.png) [@debasish283](https://discuss.elastic.co/u/debasish283)\
**Post date:** [November 11, 2019, 1:19pm UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333/3 "2019-11-11T13:19:52Z")

</div>

@abdon Thanks, thats what I was looking for....

However, I am also facing another issue i.e.if in item\_name I have something like

`item_name: (Free disk space on /boot)`

I am getting the below error

```
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "Failed to parse query [host_name: (Zabbix server) AND item_name: (Free disk space on /boot)]",
        "index_uuid": "buJiZeTmRu2CnZXXOWlVSw",
        "index": "zabbix_0"
      },
      {
        "type": "query_shard_exception",
        "reason": "Failed to parse query [host_name: (Zabbix server) AND item_name: (Free disk space on /boot)]",
        "index_uuid": "m8TdpmVQRbqg4PaqpZFuXA",
        "index": "zabbixbeat-6.8.4-0001.01.01"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "zabbix_0",
        "node": "FeQAdMiaTCmacOK_cl6nIQ",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Failed to parse query [host_name: (Zabbix server) AND item_name: (Free disk space on /boot)]",
          "index_uuid": "buJiZeTmRu2CnZXXOWlVSw",
          "index": "zabbix_0",
          "caused_by": {
            "type": "parse_exception",
            "reason": "Cannot parse 'host_name: (Zabbix server) AND item_name: (Free disk space on /boot)': Lexical error at line 1, column 69. Encountered: <EOF> after : \"/boot)\"",
            "caused_by": {
              "type": "token_mgr_error",
              "reason": "Lexical error at line 1, column 69. Encountered: <EOF> after : \"/boot)\""
            }
          }
        }
      },
      {
        "shard": 0,
        "index": "zabbixbeat-6.8.4-0001.01.01",
        "node": "FeQAdMiaTCmacOK_cl6nIQ",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Failed to parse query [host_name: (Zabbix server) AND item_name: (Free disk space on /boot)]",
          "index_uuid": "m8TdpmVQRbqg4PaqpZFuXA",
          "index": "zabbixbeat-6.8.4-0001.01.01",
          "caused_by": {
            "type": "parse_exception",
            "reason": "Cannot parse 'host_name: (Zabbix server) AND item_name: (Free disk space on /boot)': Lexical error at line 1, column 69. Encountered: <EOF> after : \"/boot)\"",
            "caused_by": {
              "type": "token_mgr_error",
              "reason": "Lexical error at line 1, column 69. Encountered: <EOF> after : \"/boot)\""
            }
          }
        }
      }
    ]
  },
  "status": 400
}

```

can you please help me on this?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 11, 2019, 1:37pm UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333/4 "2019-11-11T13:37:44Z")

</div>

Sure. The `/` is a special character that needs to be escaped. Try this query:

```auto
"item_name: (Free disk space on \\/boot)"

```

As you have noticed, the Lucene query syntax is a difficult to use. I'm not sure why your requirement would be to do this with Lucene queries. 🙂

---

<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:** [December 9, 2019, 1:37pm UTC](https://discuss.elastic.co/t/lucene-query-for-and-operator-is-not-working/207333/5 "2019-12-09T13:37:47Z")

</div>

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