how to get all documents by index in Easticsearch using NEST? - elasticsearch

I want to GET all my documents by Index. I have tried the following:
var response = client.Search(s => s.Index("test").MatchAll());
the response returns "successful operation" but it hits no document despite the fact that there are many documents under that index.

To get all documents within an index, you'll want to use the Scroll API. Note that depending on how many documents we're talking about, it's likely that you'll receive them in batches through multiple HTTP requests/responses.
There's a helper in NEST for making this easier, ScrollAll()
Time processTimePerScroll = "20s";
int numberOfSlices = Environment.ProcessorCount;
var scrollAllObservable = client.ScrollAll<Person>(processTimePerScroll, numberOfSlices, sc => sc
.MaxDegreeOfParallelism(numberOfSlices)
.Search(s => s
.Query(q => q
.MatchAll()
)
)
)
var waitHandle = new ManualResetEvent(false);
Exception exception = null;
var scrollAllObserver = new ScrollAllObserver<Person>(
onNext: response =>
{
// do something with the documents
var documents = response.SearchResponse.Documents;
},
onError: e =>
{
exception = e;
waitHandle.Set();
},
onCompleted: () => waitHandle.Set()
);
scrollAllObservable.Subscribe(scrollAllObserver);
waitHandle.WaitOne();
if (exception != null)
{
throw exception;
}

Related

Elasticsearch BulkAll with NEST: Maximum capacity exceeded

I'm using the following code to bulkindex documents. It works for everything except my Product model, but only when I try to index more than a few documents. If I do only 1 document it works fine. If I do 10, it fails. My Product model is not very complex, but it does have some nested documents with infinite self-referencing loops, but I did add ReferenceLoopHandling.Ignore to handle it.
public bool BulkIndex<T>(IEnumerable<T> items) where T : class
{
var waitHandle = new CountdownEvent(1);
var bulkAll = _client.BulkAll(items, b => b
.BackOffRetries(2)
.BackOffTime(TimeSpan.FromSeconds(5))
.RefreshOnCompleted(true)
.MaxDegreeOfParallelism(4)
.Size(100)
.Index(typeof(T).Name.ToLower())
);
bulkAll.Subscribe(new BulkAllObserver(
onNext: (b) => { Console.Write("."); },
onError: (e) => { throw e; },
onCompleted: () => waitHandle.Signal()
));
waitHandle.Wait();
return true;
}
new JsonNetSerializer(builtInSerializer, connectionSettings, () => new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}))

How Can i Search on Elasticsearch 5x

I created an object and i want to search this object as an fields value. There is no problem Elasticsearch connection. also i can get all data when i use AllIndices. but i cannot get a value when search as a field. What is the problem? i follow the own Elasticsearch document.
This is My Code
var person = new Person
{
Id = 3,
Firstname = "TOM",
Lastname = "Brandly"
};
if (!client.IndexExists("person").Exists)
{
client.CreateIndex("person");
}
var indexResponse = client.Index(person);
var searchResult = client.Search<Person>(s => s
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Firstname, "TOM")));

Angular5 RXJS recursive http requests

I currently have this situation:
#Service My Service
private users = ['user1','user2'];
//Generate list of requests to join
private getHttpList(): any[] {
let gets = new Array();
for(let index in this.users)
gets.push(this.http.get('https://api.github.com/users/' + this.users[index]))
return gets;
}
...
getList(): Observable<any[]> {
return forkJoin(this.getHttpList())
}
And in my component, I do the subscribe
this.MyService.getList().subscribe(results => {
for(let res in results) {
//...Do something here
//..I wanna do the get in of https://api.github.com/users/{user}/starred
}
})
Suppose that I just know that the "starred url" after the result of getList(), how to I can "synchronous" this part, or what's the correct form to do this?
**I try do it hardcoded --Result id wrong, because the "res" is a "interable"
this.MyService.getList().subscribe(results => {
let url = 'https://api.github.com/users/';
for(let res in results) {//This don't do the things "synchronous"
this.http.get(url + res.login +'/starred').catch(err => {
throw new Error(err.message);
}).subscribe(starred_res => {
//So we set the starred_list
res.starred_list = starred_res
})
}
})
Thanks...
As I understand you want to get starred list for every user.
The simplest way is to get all starred lists and match them with users result.
// Get users
this.MyService.getList().subscribe((results: any[]) => {
const url = 'https://api.github.com/users/';
// Create requests to get starred list for every user
const starredRequests = results.map(
res => this.http.get('https://api.github.com/users/' + res.login + '/starred')
);
// Wait when all starred requests done and map them with results array
Observable.forkJoin(starredRequests).subscribe(starred => {
results.forEach((res, index) => {
res.starred_list = starred[index];
});
console.log(results);
});
});

Elasticsearch Nest - Querying Aliases

Does the Elasticsearch NEST API expose access to /{index}/{_aliases}/*? I am trying to get a list of indexes mapped to a given alias and I cannot seem to find an appropriate method.
{
"ntdev-events017-v1": {
"aliases": {
"ntdev-events017": {}
}
}
}
http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
You can use GetAlias method on ElasticClient.
Take a look on this example:
var indexName = "sampleindex";
var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).SetDefaultIndex(indexName).EnableTrace();
var client = new ElasticClient(settings);
client.CreateIndex(descriptor => descriptor.Index(indexName));
var putAliasResponse = client.PutAlias(descriptor => descriptor
.Index(indexName).Name("alias1"));
var putAliasResponse2 = client.PutAlias(descriptor => descriptor
.Index(indexName).Name("alias2"));
var aliasesForIndex = client.GetAlias(descriptor => descriptor
.Index(indexName))
.Indices[indexName]
.Select(x => x.Name).ToList();
var indexesMappedToAlias = client.GetAlias(descriptor => descriptor.Alias("alias2"))
.Indices.Select(x => x.Key).ToList();

ReactiveUI Testing

I am attempting to see if the results of a view model are performing the correct actions.
My observables are setup as follows:
public FilterBoxViewModel()
{
var asyncFilterResults = this.filterItemsCommand.RegisterAsyncTask(x =>
this.PerformFilter(x as string));
this.filteredItems = new ObservableAsPropertyHelper<IEnumerable<IFilterBoxItem>>(
asyncFilterResults, _ => this.RaisePropertyChanged("FilteredItems"));
this.WhenAnyValue(x => x.SearchTerm)
.Throttle(TimeSpan.FromMilliseconds(50))
.Skip(1)
.Subscribe(this.filterItemsCommand.Execute);
}
Then further down I have
private async Task<IEnumerable<IFilterBoxItem>> PerformFilter(string searchTerm)
{
if (string.IsNullOrWhiteSpace(searchTerm))
{
return Enumerable.Empty<IFilterBoxItem>();
}
// Perform getting the items on the main thread and async await the results.
// This is provide a immutable version of the results so we don't cause
// threading issues.
var items = await Observable.Start(
() => this.FilterBoxManager.RootElements.GetAllItemsEnumerable()
.ToList().Select(x => new { Name = x.Name, Item = x }),
RxApp.MainThreadScheduler);
return
items.Where(x =>
x.Name.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0)
.Select(x => x.Item);
}
In my test, I am running the test schedular and advancing it, however, I am getting the PerformFilter performing at different times than I expect
eg my test is:
(new TestScheduler()).With(scheduler =>
{
var viewModel = new FilterBoxViewModel();
var testManager = new TestManager { RootElements = this.sampleItems };
viewModel.FilterBoxManager = testManager;
viewModel.SearchTerm = "folder";
scheduler.AdvanceBy(TimeSpan.FromMilliseconds(51).Ticks);
Assert.AreEqual(viewModel.FilteredItems.Select(x => x.Name), folderSearchResults);
viewModel.SearchTerm = "apple";
Assert.AreEqual(viewModel.FilteredItems.Select(x => x.Name), appleSearchResults);
});
How do I make the tester more predictable?
I am running ReactiveUI 5.5.1 and in a XAML application.
Your Throttle doesn't set a scheduler, this is a classic TestScheduler mistake

Resources