|
1 | | -:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/2.3 |
2 | | - |
3 | | -:github: https://github.com/elastic/elasticsearch-net |
4 | | - |
5 | | -:nuget: https://www.nuget.org/packages |
6 | | - |
7 | | -[[indices-paths]] |
8 | | -== Indices paths |
9 | | - |
10 | | -Some API's in Elasticsearch take one or many index name or a special `_all` marker to send the request to all the indices |
11 | | -In nest this is encoded using `Indices`. |
12 | | - |
13 | | -=== Implicit Conversion |
14 | | - |
15 | | -Several types implicitly convert to `Indices` |
16 | | - |
17 | | -[source,csharp] |
18 | | ----- |
19 | | -Nest.Indices singleIndexFromString = "name"; |
20 | | -Nest.Indices multipleIndicesFromString = "name1, name2"; |
21 | | -Nest.Indices allFromString = "_all"; |
22 | | -Nest.Indices allWithOthersFromString = "_all, name2"; |
23 | | -singleIndexFromString.Match( |
24 | | - all => all.Should().BeNull(), |
25 | | - many => many.Indices.Should().HaveCount(1).And.Contain("name") |
26 | | -); |
27 | | -multipleIndicesFromString.Match( |
28 | | - all => all.Should().BeNull(), |
29 | | - many => many.Indices.Should().HaveCount(2).And.Contain("name2") |
30 | | -); |
31 | | -allFromString.Match( |
32 | | - all => all.Should().NotBeNull(), |
33 | | - many => many.Indices.Should().BeNull() |
34 | | -); |
35 | | -allWithOthersFromString.Match( |
36 | | - all => all.Should().NotBeNull(), |
37 | | - many => many.Indices.Should().BeNull() |
38 | | -); |
39 | | ----- |
40 | | - |
41 | | -[[nest-indices]] |
42 | | -=== Using Nest.Indices |
43 | | - |
44 | | -To ease creating `IndexName` or `Indices` from expressions, there is a static `Nest.Indices` class you can use |
45 | | - |
46 | | -[source,csharp] |
47 | | ----- |
48 | | -var all = Nest.Indices.All; <1> |
49 | | -
|
50 | | -var many = Nest.Indices.Index("name1", "name2"); <2> |
51 | | -
|
52 | | -var manyTyped = Nest.Indices.Index<Project>().And<CommitActivity>(); <3> |
53 | | -
|
54 | | -var singleTyped = Nest.Indices.Index<Project>(); |
55 | | -
|
56 | | -var singleString = Nest.Indices.Index("name1"); |
57 | | -
|
58 | | -var invalidSingleString = Nest.Indices.Index("name1, name2"); <4> |
59 | | ----- |
60 | | -<1> Using `_all` indices |
61 | | - |
62 | | -<2> specifying multiple indices using strings |
63 | | - |
64 | | -<3> specifying multiple using types |
65 | | - |
66 | | -<4> an **invalid** single index name |
67 | | - |
| 1 | +:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/2.3 |
| 2 | + |
| 3 | +:github: https://github.com/elastic/elasticsearch-net |
| 4 | + |
| 5 | +:nuget: https://www.nuget.org/packages |
| 6 | + |
| 7 | +[[indices-paths]] |
| 8 | +== Indices paths |
| 9 | + |
| 10 | +Some APIs in Elasticsearch take one or many index name or a special `_all` marker to send the request to all the indices |
| 11 | +In nest this is encoded using `Indices`. |
| 12 | + |
| 13 | +=== Implicit Conversion |
| 14 | + |
| 15 | +Several types implicitly convert to `Indices` |
| 16 | + |
| 17 | +[source,csharp] |
| 18 | +---- |
| 19 | +Nest.Indices singleIndexFromString = "name"; |
| 20 | +Nest.Indices multipleIndicesFromString = "name1, name2"; |
| 21 | +Nest.Indices allFromString = "_all"; |
| 22 | +Nest.Indices allWithOthersFromString = "_all, name2"; |
| 23 | +singleIndexFromString.Match( |
| 24 | + all => all.Should().BeNull(), |
| 25 | + many => many.Indices.Should().HaveCount(1).And.Contain("name") |
| 26 | +); |
| 27 | +multipleIndicesFromString.Match( |
| 28 | + all => all.Should().BeNull(), |
| 29 | + many => many.Indices.Should().HaveCount(2).And.Contain("name2") |
| 30 | +); |
| 31 | +allFromString.Match( |
| 32 | + all => all.Should().NotBeNull(), |
| 33 | + many => many.Indices.Should().BeNull() |
| 34 | +); |
| 35 | +allWithOthersFromString.Match( |
| 36 | + all => all.Should().NotBeNull(), |
| 37 | + many => many.Indices.Should().BeNull() |
| 38 | +); |
| 39 | +---- |
| 40 | + |
| 41 | +[[nest-indices]] |
| 42 | +=== Using Nest.Indices |
| 43 | + |
| 44 | +To ease creating `IndexName` or `Indices` from expressions, there is a static `Nest.Indices` class you can use |
| 45 | + |
| 46 | +==== Specifying a single index |
| 47 | + |
| 48 | +A single index can be specified using a CLR type or a string |
| 49 | + |
| 50 | +[source,csharp] |
| 51 | +---- |
| 52 | +var client = TestClient.GetInMemoryClient(); |
| 53 | +
|
| 54 | +var singleString = Nest.Indices.Index("name1"); <1> |
| 55 | +
|
| 56 | +var singleTyped = Nest.Indices.Index<Project>(); <2> |
| 57 | +
|
| 58 | +ISearchRequest singleStringRequest = new SearchDescriptor<Project>().Index(singleString); |
| 59 | +
|
| 60 | +ISearchRequest singleTypedRequest = new SearchDescriptor<Project>().Index(singleTyped); |
| 61 | +
|
| 62 | +((IUrlParameter)singleStringRequest.Index).GetString(client.ConnectionSettings).Should().Be("name1"); |
| 63 | +
|
| 64 | +((IUrlParameter)singleTypedRequest.Index).GetString(client.ConnectionSettings).Should().Be("project"); |
| 65 | +
|
| 66 | +var invalidSingleString = Nest.Indices.Index("name1, name2"); <3> |
| 67 | +---- |
| 68 | +<1> specifying a single index using a string |
| 69 | + |
| 70 | +<2> specifying a single index using a type |
| 71 | + |
| 72 | +<3> an **invalid** single index name |
| 73 | + |
| 74 | +==== Specifying multiple indices |
| 75 | + |
| 76 | +Similarly to a single index, multiple indices can be specified using multiple CLR types and multiple strings |
| 77 | + |
| 78 | +[source,csharp] |
| 79 | +---- |
| 80 | +var client = TestClient.GetInMemoryClient(); |
| 81 | +
|
| 82 | +var manyStrings = Nest.Indices.Index("name1", "name2"); <1> |
| 83 | +
|
| 84 | +var manyTypes = Nest.Indices.Index<Project>().And<Developer>(); <2> |
| 85 | +
|
| 86 | +ISearchRequest manyStringRequest = new SearchDescriptor<Project>().Index(manyStrings); |
| 87 | +
|
| 88 | +ISearchRequest manyTypedRequest = new SearchDescriptor<Project>().Index(manyTypes); |
| 89 | +
|
| 90 | +((IUrlParameter)manyStringRequest.Index).GetString(client.ConnectionSettings).Should().Be("name1,name2"); |
| 91 | +
|
| 92 | +((IUrlParameter)manyTypedRequest.Index).GetString(client.ConnectionSettings).Should().Be("project,devs"); <3> |
| 93 | +---- |
| 94 | +<1> specifying multiple indices using strings |
| 95 | + |
| 96 | +<2> specifying multiple indices using types |
| 97 | + |
| 98 | +<3> The index names here come from the Connection Settings passed to `TestClient`. See the documentation on <<index-name-inference, Index Name Inference>> for more details. |
| 99 | + |
| 100 | +==== Specifying All Indices |
| 101 | + |
| 102 | +Elasticsearch allows searching across multiple indices using the special `_all` marker. |
| 103 | + |
| 104 | +NEST exposes `_all` with `Indices.All` and `Indices.AllIndices`. Why expose it in two ways, you ask? |
| 105 | +Well, you may be using both `Nest.Indices` and `Nest.Types` in the same file and you may also be using C#6 |
| 106 | +static imports too; in this scenario, `All` becomes ambiguous between `Indices.All` and `Types.All`, so the`_all` marker is exposed as `Indices.AllIndices` to alleviate this ambiguity |
| 107 | + |
| 108 | +[source,csharp] |
| 109 | +---- |
| 110 | +var client = TestClient.GetInMemoryClient(); |
| 111 | +
|
| 112 | +var indicesAll = Nest.Indices.All; |
| 113 | +
|
| 114 | +var allIndices = Nest.Indices.AllIndices; |
| 115 | +
|
| 116 | +ISearchRequest indicesAllRequest = new SearchDescriptor<Project>().Index(indicesAll); |
| 117 | +
|
| 118 | +ISearchRequest allIndicesRequest = new SearchDescriptor<Project>().Index(allIndices); |
| 119 | +
|
| 120 | +((IUrlParameter)indicesAllRequest.Index).GetString(client.ConnectionSettings).Should().Be("_all"); |
| 121 | +
|
| 122 | +((IUrlParameter)allIndicesRequest.Index).GetString(client.ConnectionSettings).Should().Be("_all"); |
| 123 | +---- |
| 124 | + |
|
0 commit comments