Single field with multiple values

Hi all

How to write single field with multiple value in nested query.

example : node.name ["a","b"]

"nested" : {
"query" : {
"bool" : {
"must" : [
{
"match" : {
"services.code" : {
"query" : "bbbbbbbb",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
{
"match" : {
"services.code" : {
"query" : "aaaaaaaa",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

Coming back to your question, have a look at multi_match query.

Thank you,

Here is my code for reference.

				BoolQueryBuilder co = QueryBuilders.boolQuery();
				List<String> codes = new ArrayList<String>();
				
				for(HashMap<String, LineItem> map : a){
					LineItem  l = map.values().iterator().next();
					codes.add(l.getCode());
//					co.must(QueryBuilders.multiMatchQuery("services.service_code", l.getCode()));
				}
				
				BoolQueryBuilder co = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("services.service_code", Arrays.asList(codes)));

				NestedQueryBuilder serviceCode = QueryBuilders.nestedQuery("services", co, ScoreMode.Max)
						.innerHit(new InnerHitBuilder("services").addDocValueField("base")
								.addDocValueField("current"))
						;

				BoolQueryBuilder q1 = new BoolQueryBuilder().must(serviceCode);
				SortBuilder<?> sort = SortBuilders.geoDistanceSort("address.latlon", Double.valueOf(lat), Double.valueOf(lon)).setNestedSort(new NestedSortBuilder("address")).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS);

				String[] sourceInclude = { "name", "code", "profile_pic", "avg_rating" };
				String[] sourceExclude = {}; 
				response = client.prepareSearch("associate_information").setFetchSource(sourceInclude, sourceExclude).setQuery(q1).addSort(sort).get();

In the code i am passing the codes as list .

problems

  1. if i send 10 codes the response is coming only few codes.(if i send individual code getting response)

  2.  				for(HashMap<String, LineItem> map : a){
     					LineItem  l = map.values().iterator().next();
     					codes.add(l.getCode());
     //					co.must(QueryBuilders.multiMatchQuery("services.service_code", l.getCode()));
     				}
    

if i prepare query like this i am not getting empty hits.

how i can pass list as input.

Sorry. I misunderstood your initial question.
The multiMatchQuery is made to match a text on multiple fields.
You want the opposite: one field and multiple values.

When you wrote node.name ["a","b"], do you want the document to have both values or one of them only at least (the more, the better)?

I mean if i send one by one as input, need to execute more number of times to fetch the data.

each and every code have a data in elasticsearch. so that i passed Codes as list.

I don't understand your answer.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 361,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "associate_information",
        "_type" : "_doc",
        "_id" : "PL00000000000120_3357",
        "_score" : 1.0,
        "_source" : {
          "description" : "jjgkuhb",
          "mobile_number" : "1111",
          "status" : true,
          "created_date" : "26/12/2018 12:00:14",
          "branch_id" : "1245",
          "address" : {
            "locality" : "222",
            "area" : "Visakhapatnam",
            "city" : "Visakhapatnam",
            "district" : "Vishakhapatnam",
            "state" : "Andhra Pradesh",
            "country" : "India",
            "zipcode" : "530016",
            "latlon" : {
              "lat" : "17.7383557",
              "lon" : "83.30154010000001"
            }
          },
          "avg_rating" : 0,
          "no_of_ratings" : 0,
          "no_of_reviews" : 0,
          "liked_by" : 0,
          "services" : [
            {
              "name" : "trainers",
              "code" : "00003",
              "engage_at" : "1"
            },
            {
              "name" : "Aerobics trainers",
              "code" : "00008",
              "engage_at" : "1"
            }
            
          ],
          "available_timings" : [ ],
          "audio" : false,
          "video" : false,
          "branch_type" : "B2C",
          "corporate_name" : "",
          "roles" : [ ]
        }
      },
      {
        "_index" : "associate_information",
        "_type" : "_doc",
        "_id" : "HO00000009990112_3748",
        "_score" : 1.0,
        "_source" : {
          "description" : "",
          "mobile_number" : "2222222",
          "status" : true,
          "created_date" : "21/02/2019 15:03:37",
          "branch_id" : "3748",
          "address" : {
            "locality" : "hyderabad",
            "area" : "hyderabad",
            "city" : "hyderabad",
            "district" : "Ranga Reddy",
            "state" : "Telangana",
            "country" : "India",
            "zipcode" : "500018",
            "latlon" : {
              "lat" : "17.385044",
              "lon" : "78.486671"
            }
          },
          "avg_rating" : 0,
          "no_of_ratings" : 0,
          "no_of_reviews" : 0,
          "liked_by" : 0,
          "services" : [
            {
              "name" : "ssss",
              "code" : "0001",
              "engage_at" : "2"
            },
            {
              "name" : "ABD",
              "code" : "0002",
              "engage_at" : "2"
            },
            {
              "name" : "AIN",
              "code" : "0003",
              "engage_at" : "2"
            },
            {
              "name" : "UPPER",
              "code" : "0004",
              "price" : "0",
              "cprice" : "600",
            }
          ],
          "available_timings" : [ ],
          "is_internal_doctor" : false,
          "audio" : false,
          "video" : false,
          "branch_type" : "B2C",
          "corporate_name" : "",
          "roles" : [ ]
        }
      },
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {}
      }
    ]
  }
}
  1. based on lat , lon i am searching a doc with in the doc
  2. in that doc i am searching for the codes with in services.

Ok. Could you share a simple script I can use to reproduce your problem and give you a solution?

But some answers:

  1. https://www.elastic.co/guide/en/elasticsearch/reference/6.6/geo-queries.html
  2. https://www.elastic.co/guide/en/elasticsearch/reference/6.6/term-level-queries.html

You can put all in a Bool query.

{
  "bool" : {
    "must" : [
      {
        "nested" : {
          "query" : {
            "bool" : {
              "must" : [
                {
                  "match" : {
                    "services.code" : {
                      "query" : [
                        [
                          "0001",
                          "0002",
                          "0003"                    
                        ]
                      ],
                      "operator" : "OR",
                      "prefix_length" : 0,
                      "max_expansions" : 50,
                      "fuzzy_transpositions" : true,
                      "lenient" : false,
                      "zero_terms_query" : "NONE",
                      "auto_generate_synonyms_phrase_query" : true,
                      "boost" : 1.0
                    }
                  }
                }
              ],
              "adjust_pure_negative" : true,
              "boost" : 1.0
            }
          },
          "path" : "services",
          "ignore_unmapped" : false,
          "score_mode" : "max",
          "boost" : 1.0,
          "inner_hits" : {
            "name" : "services",
            "ignore_unmapped" : false,
            "from" : 0,
            "size" : 3,
            "version" : false,
            "explain" : false,
            "track_scores" : false,
            "docvalue_fields" : [
              "price",
              "cprice"
            ]
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

sort query we are using is

{
          "_geo_distance" : {
            "address.latlon" : [
              {
                "lat" : 17.4501615,
                "lon" : 78.3671454
              }
            ],
            "unit" : "km",
            "distance_type" : "arc",
            "order" : "asc",
            "nested" : {
              "path" : "address"
            },
            "validation_method" : "STRICT",
            "ignore_unmapped" : false
          }
        }

I can't reproduce anything with this I'm afraid. Please read the link I pasted here:

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