# Having difficulty creating multiple field queries

**URL:** <https://discuss.elastic.co/t/having-difficulty-creating-multiple-field-queries/297009>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [February 11, 2022, 5:49pm UTC](https://discuss.elastic.co/t/having-difficulty-creating-multiple-field-queries/297009 "2022-02-11T17:49:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)\
**Post date:** [February 11, 2022, 5:49pm UTC](https://discuss.elastic.co/t/having-difficulty-creating-multiple-field-queries/297009/1 "2022-02-11T17:49:26Z")

</div>

I'm attempting to create slightly more complex queries using Java API client 7.16 (like the json below), but I'm not sure which direction to go in to do this correctly. Do I use terms query for something like this query below, or do I add multiple terms to a query?

Also, it's difficult to navigate the docs for this version, so I can't find a way to even specify multiple terms via a list.  
The below java code only works for "keyword" types, but doesn't work for any text fields - is there something I'm missing? I tried attaching an analyzer but the client threw some errors there.  
My ideal scenario would be providing a map of queries `{ "textField1": "foo", "boolField1": "True", "intField1": "20", "keywordField1": "Af:5E:1b:D8", "obj1.name": "bar"}` and using the search response to fetch all the matching documents (note: the query could contain nested fields and non-nested fields). Does this exist? What is the best way to do something like this if it does not?

Thank you in advance.

```java
SearchRequest sr = new SearchRequest.Builder()
            .index("index")
            .query(q -> q
                .term(t -> t
                    .field("field1")
                    .value(v -> v.stringValue("foobar"))
                )).build();

```

```json
{
    "bool":{
        "must":[
            {"match": {"textField1": "foo"}},
            {"match": {"boolField1": True}},
            {"match": {"intField1": 20}},
            {"match": {"keywordField1": "Af:5E:1b:D8"}},
            {
                "nested": {
                    "path": "obj1",
                    "query": {
                        "bool": {
                            "must": [
                                { "match": { "obj1.name": "bar" } }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)\
**Post date:** [February 13, 2022, 7:29pm UTC](https://discuss.elastic.co/t/having-difficulty-creating-multiple-field-queries/297009/2 "2022-02-13T19:29:34Z")

</div>

Update: I found that QueryString with lucene syntax fits my use case far better in case anyone is trying to do something similar.  
Also, I realized that writing out the builders in similar to how it appears in the actual JSON query makes using the Java API client much more straight forward.

---

<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:** [March 13, 2022, 7:29pm UTC](https://discuss.elastic.co/t/having-difficulty-creating-multiple-field-queries/297009/3 "2022-03-13T19:29:52Z")

</div>

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